Styling individual tabs in a TabBar control

by Peter deHaan on November 19, 2007

in TabBar

The following example shows how you can 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.

A big thanks to Joan for helping me figure it out.

Full code after the jump.

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2007/11/19/styling-individual-tabs-in-a-tabbar-control/ -->
<mx:Application name="TabBar_getChildAt_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">
 
    <mx:Script>
    <![CDATA[
        import mx.events.ItemClickEvent;
        import mx.controls.tabBarClasses.Tab;
 
        private function tabBar_creationComplete():void {
            var colorArr:Array = ["red", "haloOrange", "yellow", "haloGreen", "haloBlue"];
            var color:String;
            var tab:Tab;
            var idx:uint;
            var len:uint = tabBar.dataProvider.length;
 
            for (idx = 0; idx < len; idx++) {
                var i:int = idx % colorArr.length;
                color = colorArr[i];
                tab = Tab(tabBar.getChildAt(idx));
                tab.setStyle("fillColors", [color, "white"]);
                tab.setStyle("fillAlphas", [1.0, 1.0]);
                tab.setStyle("backgroundColor", color);
            }
        }
 
        private function tabBar_itemClick(evt:ItemClickEvent):void {
            viewStack.selectedIndex = evt.index;
        }
    ]]>
    </mx:Script>
 
    <mx:Array id="arr">
        <mx:Object label="Red" />
        <mx:Object label="Orange" />
        <mx:Object label="Yellow" />
        <mx:Object label="Green" />
        <mx:Object label="Blue" />
    </mx:Array>
 
    <mx:TabBar id="tabBar"
            dataProvider="{arr}"
            creationComplete="tabBar_creationComplete();"
            itemClick="tabBar_itemClick(event);" />
 
    <mx:ViewStack id="viewStack"
            width="{tabBar.width}"
            styleName="plain">
        <mx:VBox id="redVBox" width="100%" height="100">
            <mx:Label text="Red VBox" />
        </mx:VBox>
        <mx:VBox id="orangeVBox" width="100%" height="100">
            <mx:Label text="Orange VBox" />
        </mx:VBox>
        <mx:VBox id="yellowVBox" width="100%" height="100">
            <mx:Label text="Yellow VBox" />
        </mx:VBox>
        <mx:VBox id="greenVBox" width="100%" height="100">
            <mx:Label text="Green VBox" />
        </mx:VBox>
        <mx:VBox id="blueVBox" width="100%" height="100">
            <mx:Label text="Blue VBox" />
        </mx:VBox>
    </mx:ViewStack>
 
</mx:Application>

View source is enabled in the following example.

{ 4 comments… read them below or add one }

1 Rob Sherman December 19, 2007 at 12:57 pm

I’d sure like to see the swf that goes with this..

Reply

2 Mark Thomas September 4, 2009 at 12:56 am

I can’t seem to get this to work. I’m using Flex 3 and Flash player 10.0. When I copy and paste the code verbatim into Flex builder and run it, I get a gray box with text-only tabs (no colors, no boarders, nothing). Any ideas?

Reply

3 Peter deHaan September 4, 2009 at 6:58 am

@Mark Thomas,

Which version/build of the Flex SDK are you using? I tried w/ 3.4.0.9147 and it looks the same as the example above.

Peter

Reply

4 Vikram Malhotra December 18, 2009 at 11:12 pm

How to get a similar thing working for TabNavigator?

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: