Changing the label placement for a Flex ButtonBar control

by Peter deHaan on January 8, 2008

in ButtonBar

The following example shows how you can change the button’s label placement relative to its icon in a ButtonBar control in Flex by using the getChildAt() method and labelPlacement property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/08/changing-the-label-placement-for-a-flex-buttonbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.Button;
            import mx.controls.ButtonLabelPlacement;

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

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

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

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

            private function init():void {
                var idx:uint;
                var len:uint = buttonBar.numChildren;
                var btn:Button;
                /* Loop over each button. */
                for (idx = 0; idx < len; idx++) {
                    /* Get reference to current Button. */
                    btn = buttonBar.getChildAt(idx) as Button;
                    /* Set labelPlacement property to current item in
                       arr Array object. */
                    btn.labelPlacement = arr[idx].label;
                }
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="left" img="{arrowLeft}" />
        <mx:Object label="right" img="{arrowRight}" />
        <mx:Object label="top" img="{arrowUp}" />
        <mx:Object label="bottom" img="{arrowDown}" />
    </mx:Array>

    <mx:ButtonBar id="buttonBar"
            dataProvider="{arr}"
            iconField="img"
            buttonHeight="64"
            width="500"
            creationComplete="init();" />

</mx:Application>

View source is enabled in the following example.

{ 3 comments… read them below or add one }

1 Charitha March 24, 2008 at 11:45 pm

Thank you very much. I was searching for such thing. Tried to set it using CSS but it didn’t work. This works perfectly. Grate code..

Reply

2 Ankush September 24, 2009 at 8:06 am

awesome!! Thanks

Reply

3 marcus December 8, 2009 at 9:43 pm

beautiful work. keep it up!

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: