Adding icons to the Flex ButtonBar control

by Peter deHaan on January 7, 2008

in ButtonBar

The following example shows how you can add icons to the ButtonBar control in Flex.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/07/adding-icons-to-the-flex-buttonbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

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

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

            [Bindable]
            [Embed("assets/ColorPicker.png")]
            private var colorPickerIcon:Class;
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="Button" icon="{buttonIcon}" />
        <mx:Object label="ButtonBar" icon="{buttonBarIcon}" />
        <mx:Object label="CheckBox" icon="{checkBoxIcon}" />
        <mx:Object label="ColorPicker" icon="{colorPickerIcon}" />
    </mx:Array>

    <!-- The data provider has an "icon" property. -->
    <mx:ButtonBar id="buttonBar"
            dataProvider="{arr}"
            height="32" />

</mx:Application>

View source is enabled in the following example.

The previous example works because the the data provider has an “icon” field, and the default value for the ButtonBar control’s iconField property is “icon”.

The following snippet shows how you can override the default value for the ButtonBar control’s iconField property in MXML:

<mx:Array id="arr">
    <mx:Object label="Button" img="{buttonIcon}" />
    <mx:Object label="ButtonBar" img="{buttonBarIcon}" />
    <mx:Object label="CheckBox" img="{checkBoxIcon}" />
    <mx:Object label="ColorPicker" img="{colorPickerIcon}" />
</mx:Array>

<!-- The data provider has an "img" property, so use ButtonBar
     control's iconField property to set the icons on the
     control. -->
<mx:ButtonBar id="buttonBar"
        dataProvider="{arr}"
        iconField="img"
        height="32" />

You could also set the iconField property using ActionScript, as shown in the following snippet:

<mx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        private function buttonBar_initialize(evt:FlexEvent):void {
            buttonBar.iconField = "img";
        }
    ]]>
</mx:Script>

<mx:Array id="arr">
    <mx:Object label="Button" img="{buttonIcon}" />
    <mx:Object label="ButtonBar" img="{buttonBarIcon}" />
    <mx:Object label="CheckBox" img="{checkBoxIcon}" />
    <mx:Object label="ColorPicker" img="{colorPickerIcon}" />
</mx:Array>

<mx:ButtonBar id="buttonBar"
        dataProvider="{arr}"
        height="32"
        initialize="buttonBar_initialize(event);" />

Or, the slightly more condensed version:

<mx:Array id="arr">
    <mx:Object label="Button" img="{buttonIcon}" />
    <mx:Object label="ButtonBar" img="{buttonBarIcon}" />
    <mx:Object label="CheckBox" img="{checkBoxIcon}" />
    <mx:Object label="ColorPicker" img="{colorPickerIcon}" />
</mx:Array>

<mx:ButtonBar id="buttonBar"
        dataProvider="{arr}"
        height="32"
        initialize="buttonBar.iconField = 'img';" />

{ 17 comments… read them below or add one }

1 dormouse January 8, 2008 at 5:31 am

very good

enjoy

Reply

2 lili January 11, 2008 at 9:56 pm

Thank you you example it’s give me a lot of help

Reply

3 jeremie March 20, 2008 at 7:20 am

Thanks for the very good tutorial,

I have a question. If I use a XML file as data provider and a repeater class to dynamically create buttons, is there a way to add a proper icon to the dynamically created buttons.

Here is my code:

<mx:Repeater id="toolBarRepeater" dataProvider="{toolBarData}">
<mx:Button styleName="navbutton" name="{toolBarRepeater.currentItem.id}"
    click="setTransition(String(event.currentTarget.getRepeaterItem().state));"
    toolTip="{toolBarRepeater.currentItem.tooltip}"
    icon="HERE I WANT A DYNAMIC ICON"/>
</mx:Repeater>

I have an icon param in my xml but I alway have an ‘Invalid metadata format’ error.

Thx!

Reply

4 peterd March 20, 2008 at 8:15 am

jeremie,

Good question. What does your XML look like?

Peter

Reply

5 jeremie March 21, 2008 at 7:55 pm

Hi,
Here is a sample of my XML. I have tried several approach, but non of them worked. I put below what I’ve tried.

<?xml version="1.0" encoding="UTF-8"?>
<toolBar>

	<toolBarItem>
		<id>toDefaultBtn</id>
		<state>default</state>
		<tooltip>Home</tooltip>
		<icon>@Embed("assets/icons/icon.png")</icon>
	</toolBarItem>

	<toolBarItem>
		<id>toCustomersBtn</id>
		<state>customers</state>
		<tooltip>Customers</tooltip>
		<icon>{icon1}</icon>
	</toolBarItem>

	<toolBarItem>
		<id>toStockBtn</id>
		<state>stock</state>
		<tooltip>Boutique Stock</tooltip>
		<icon>icon2</icon>
	</toolBarItem>

</toolBar>

Thx for your help!

Reply

6 dc April 9, 2008 at 3:48 am

I just got the metadata issue, I had Embed(/’assets/my.gif’).
Also, not sure if it is the blog, but make sure it is normal quotes (“) rather than smart quotes

Reply

7 jeremie April 12, 2008 at 3:32 am

Hi dc
Thanks for the reply, but I am not sure to understand what u mean

Here is what I changed:

<toolBarItem>
	<id>toDefaultBtn</id>
	<state>default</state>
	<tooltip>Home</tooltip>
	<icon>@Embed(/"assets/icons/icon.png")</icon>
</toolBarItem>

And here is the same error message:
Error #1034: Type Coercion failed: cannot convert “@Embed(/”assets/icons/icon.png”)” to Class.

Could u please send me the details of what u got to work please?
Thx

Reply

8 alex July 17, 2008 at 3:14 am

what about using @Embed(‘assets/icons/icon.png’) instead

Reply

9 alex July 17, 2008 at 3:14 am

i mean: & # 6 4 ; Embed(‘images/icon8.png’)

Reply

10 Tom October 24, 2008 at 7:55 am

Actually, @Embed is like a preprocessor directive (like #include in C). @Embed means open the file, and include it in my SWF file. So you CANT use it at runtime.

There is a big problem (issue) with icon in flex, because icon must be compiled with the SWF (they not implement loader like image class).

BUT you can find utility class emulating this, and so you can use url for your icon.

Have a look at http://blog.benstucki.net/?p=42

Reply

11 Fred January 23, 2009 at 5:12 am

Do you know how to *CHANGE* the icon of a button of the ButtonBar ? I tried many piece of code, but my button do not change…

Reply

12 Peter deHaan January 23, 2009 at 9:19 am

Fred,

Try something like this:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:Script>
        <![CDATA[
            [Embed("Button.png")]
            private const ButtonIcon:Class;

            private function btn_click(evt:Event):void {
                /* Change the first button in the ButtonBar to use the embedded ButtonIcon asset. */
                Button(buttonBar .getChildAt(0)).setStyle("icon", ButtonIcon);
            }
        ]]>
    </mx:Script>

    <mx:ArrayCollection id="arr">
        <mx:Object label="Button" icon="@Embed('ButtonBar.png')" />
        <mx:Object label="ButtonBar" icon="@Embed('ButtonBar.png')" />
        <mx:Object label="CheckBox" icon="@Embed('CheckBox.png')" />
    </mx:ArrayCollection>

    <mx:ApplicationControlBar dock="true">
        <mx:Button label="Oops, I meant Button icon"
                click="btn_click(event);" />
    </mx:ApplicationControlBar>

    <mx:ButtonBar id="buttonBar " dataProvider="{arr}" />

</mx:Application>

Peter

Reply

13 Scott Macdonald April 9, 2009 at 9:01 am

The code generates an error. I placed an file named Editor.png in a folder in my Flex project named assets. When i attempted to reference it as such:

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

i got this error:

Unable to resolve /assets/Editor.png for transcoding.

2nd — which property needs to be set so that an icon appears in the application’s main toolbar?

Reply

14 Peter deHaan April 9, 2009 at 1:26 pm

Scott Macdonald,

What if you remove the “/” before the assets directory?

Peter

Reply

15 Molt July 19, 2009 at 2:37 pm

thanks alot peter,
that solved my case” though i had to add the slash :-D

Reply

16 Ranjeet April 17, 2009 at 6:37 am

I was facing a similar problem. Following format fixed it:

icon="@Embed(source='../assets/GD1.jpg')"

Reply

17 Adam J December 30, 2009 at 12:02 pm

is there a way to specify the disabled icon also?

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: