Setting the label placement on a TabBar control in Flex

by Peter deHaan on October 6, 2008

In a previous example, “Styling individual tabs in a TabBar control”, we saw how you could style individual tabs in a Flex TabBar control by calling the getChildAt() method on the tab bar, and then calling setStyle() on the returned Tab reference.

The following example shows how you can loop over the tabs in a Flex TabBar control and set each tab’s labelPlacement property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/06/setting-the-label-placement-on-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_getChildAt_labelPlacement_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.tabBarClasses.Tab;

            private function init():void {
                var tab:Tab;
                var idx:uint;
                var len:uint = tabBar.numChildren;
                for (idx=0; idx<len; idx++) {
                    tab = tabBar.getChildAt(idx) as Tab;
                    tab.labelPlacement = tab.label;
                }
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="left"
                icon="@Embed('assets/arrow_left.png')" />
        <mx:Object label="right"
                icon="@Embed('assets/arrow_right.png')" />
        <mx:Object label="top"
                icon="@Embed('assets/arrow_up.png')" />
        <mx:Object label="bottom"
                icon="@Embed('assets/arrow_down.png')" />
    </mx:Array>

    <mx:TabBar id="tabBar"
            dataProvider="{arr}"
            tabWidth="150"
            tabHeight="60"
            creationComplete="init();" />

</mx:Application>

View source is enabled in the following example.

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/06/setting-the-label-placement-on-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_getChildAt_labelPlacement_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.controls.ButtonLabelPlacement;
            import mx.controls.TabBar;
            import mx.controls.tabBarClasses.Tab;

            [Embed("assets/arrow_left.png")]
            private const ARROW_LEFT_ICON:Class;

            [Embed("assets/arrow_right.png")]
            private const ARROW_RIGHT_ICON:Class;

            [Embed("assets/arrow_up.png")]
            private const ARROW_TOP_ICON:Class;

            [Embed("assets/arrow_down.png")]
            private const ARROW_BOTTOM_ICON:Class;

            private var arr:Array;
            private var tabBar:TabBar;

            private function init():void {
                arr = [];
                arr.push({label:ButtonLabelPlacement.LEFT,
                            icon:ARROW_LEFT_ICON});
                arr.push({label:ButtonLabelPlacement.RIGHT,
                            icon:ARROW_RIGHT_ICON});
                arr.push({label:ButtonLabelPlacement.TOP,
                            icon:ARROW_TOP_ICON});
                arr.push({label:ButtonLabelPlacement.BOTTOM,
                            icon:ARROW_BOTTOM_ICON});

                tabBar = new TabBar();
                tabBar.dataProvider = arr;
                tabBar.setStyle("tabWidth", 150);
                tabBar.setStyle("tabHeight", 60);
                addChild(tabBar);

                var tab:Tab;
                var idx:uint;
                var len:uint = tabBar.numChildren;
                for (idx=0; idx<len; idx++) {
                    tab = tabBar.getChildAt(idx) as Tab;
                    tab.labelPlacement = tab.label;
                }
            }
        ]]>
    </mx:Script>

</mx:Application>

{ 2 comments… read them below or add one }

web hustler October 7, 2008 at 5:13 am

thanks for the code

Reply

Jidolstar October 8, 2008 at 6:28 pm

thanks ^^

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

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: