Displaying a main icon in a Flex PopUpButton control

by Peter deHaan on February 3, 2008

in PopUpButton

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>

{ 1 comment… read it below or add one }

1 hattori February 4, 2008 at 7:13 am

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.

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: