In a previous example, “Displaying icons in a Flex PopUpButton control”, we saw how you can display icons in a PopUpButton control’s pop up menu.

The following example shows how you can display an icon in a Flex PopUpButton control’s main button by setting the icon style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/03/displaying-a-main-icon-in-a-flex-popupbutton-control/ -->
<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.Alert;
            import mx.controls.Menu;

            [Bindable]
            [Embed("assets/asterisk_orange.png")]
            private var asteriskOrangeIcon:Class;

            [Bindable]
            private var menu:Menu;

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

    <mx:Array id="arr">
        <mx:Object label="Alert" />
        <mx:Object label="Button" />
        <mx:Object label="ButtonBar" />
        <mx:Object label="CheckBox" />
        <mx:Object label="ColorPicker" />
        <mx:Object label="ComboBox" />
    </mx:Array>

    <mx:PopUpButton id="popUpButton"
            label="Please select an item"
            icon="{asteriskOrangeIcon}"
            popUp="{menu}"
            initialize="popUpButton_initialize();" />

</mx:Application>

View source is enabled in the following example.

You could also set the icon style in an external .CSS file or in an <mx:Style /> block, as shown in the following snippet:

<mx:Style>
    PopUpButton {
        icon: Embed("assets/asterisk_orange.png");
    }
</mx:Style>

Or, you could set the icon style in ActionScript, as shown in the following snippet:

<mx:Script>
    <![CDATA[
        [Embed("assets/asterisk_orange.png")]
        private var asteriskOrangeIcon:Class;

        private function popUpButton_initialize():void {
            popUpButton.setStyle("icon", asteriskOrangeIcon);
        }
    ]]>
</mx:Script>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

0 Responses to Displaying a main icon in a Flex PopUpButton control

  1. hattori says:

    Can anyone tell me how can I change the size of closeButtonSkin of TitleWindow.

    Default,closeButtonSkin of TitleWindow is a fixed size,now I need make it bigger.

    Can anyone help me?

    Thank you.

Leave a Reply

Your email address will not be published.

You may 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