29
Jan
08

Adding a horizontal separator to a Flex PopUpButton control’s pop up menu

The following example shows how you can add a horizontal separator to a PopUpButton control’s pop up menu in Flex by setting the type attribute to “separator” in the menu’s data provider.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/29/adding-a-horizontal-separator-to-a-flex-popupbutton-controls-pop-up-menu/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Style>
        PopUpButton {
            popUpStyleName: myCustomPopUpStyleName;
        }

        .myCustomPopUpStyleName {
            fontWeight: normal;
            textAlign: left;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            import mx.controls.Menu;

            private var menu:Menu;

            private function popUpButton_initialize():void {
                menu = new Menu();
                menu.dataProvider = arr;
                popUpButton.popUp = menu;
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="One" />
        <mx:Object label="Two" />
        <mx:Object label="Three" />
        <mx:Object type="separator" />
        <mx:Object label="The quick brown fox jumped over the lazy dog." />
    </mx:Array>

    <mx:PopUpButton id="popUpButton"
            label="PopUpButton with separator"
            openAlways="true"
            initialize="popUpButton_initialize();" />

</mx:Application>

View source is enabled in the following example.

If you happen to be using an XML (or XMLList) data provider, you could use the following code instead:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/29/adding-a-horizontal-separator-to-a-flex-popupbutton-controls-pop-up-menu/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="top"
        backgroundColor="white">

    <mx:Style>
        PopUpButton {
            popUpStyleName: myCustomPopUpStyleName;
        }

        .myCustomPopUpStyleName {
            fontWeight: normal;
            textAlign: left;
        }
    </mx:Style>

    <mx:Script>
        <![CDATA[
            import mx.controls.Menu;

            private var menu:Menu;

            private function popUpButton_initialize():void {
                menu = new Menu();
                menu.labelField = "@label";
                menu.dataProvider = xml;
                popUpButton.popUp = menu;
            }
        ]]>
    </mx:Script>

    <mx:XMLList id="xml">
        <node label="One" />
        <node label="Two" />
        <node label="Three" />
        <node type="separator" />
        <node label="The quick brown fox jumped over the lazy dog." />
    </mx:XMLList>

    <mx:PopUpButton id="popUpButton"
            label="PopUpButton with separator"
            openAlways="true"
            initialize="popUpButton_initialize();" />

</mx:Application>

For an example on reducing the amount of whitespace around the separator in a Menu control, see “Adding a horizontal separator to a Flex PopUpButton control’s pop up menu (redux)”.


3 Responses to “Adding a horizontal separator to a Flex PopUpButton control's pop up menu”


  1. 1 Jed Wood Jan 30th, 2008 at 3:20 am

    I think you mean vertical?

    Keep up the great work!

  2. 2 DS Feb 1st, 2008 at 9:18 am

    You should call the popUpButton_initialize() on creationComplete since if the componenent is nested in some other components the initialization will silently fail.

  3. 3 guna Oct 2nd, 2008 at 5:13 pm

    thanks

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Firefox 2
  • Powered by Redoable 1.2
  • Feeds burnt by Feedburner
  • Feed