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

by Peter deHaan on January 29, 2008

in Menu,PopUpButton

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 comments… read them below or add one }

1 Jed Wood January 30, 2008 at 3:20 am

I think you mean vertical?

Keep up the great work!

Reply

2 DS February 1, 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.

Reply

3 guna October 2, 2008 at 5:13 pm

thanks

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: