03
Feb
08

Displaying a main icon in a Flex PopUpButton control

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 Response to “Displaying a main icon in a Flex PopUpButton control”


  1. 1 hattori Feb 4th, 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.

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;".




February 2008
M T W T F S S
« Jan   Mar »
 123
45678910
11121314151617
18192021222324
2526272829  

Badge Farm

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