Styling individual tabs in a TabBar control
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.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
14 Responses to Styling individual tabs in a TabBar control
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


I’d sure like to see the swf that goes with this..
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?
@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
How to get a similar thing working for TabNavigator?
How do we change the color of the text on the tab itself? I see stuff for the body of a tab, but not the tab header …
@Shannara,
If you want to use the same text color for each tab you can set the
colorstyle in the MXML directly, as seen in the following snippet:Or if you want a separate text colors for each tab you can set the
colorstyle using ActionScript, as seen in the following example:Peter
In this example the tabs are already instantiated when calling tabBar.getChildAt(idx). However when adding tab pages dynamically the getChildAt often causes an error because the tabs haven’t been set up yet. This makes it rather problematic to set properties (visible…) or styles.
This example works in this page, but when I run it in Flashbuilder 4, it does not work. I get the tabs but no color.
@Richard,
Try something like this instead:
Peter
Yee, the code with chromoColor is working, but: its only make one color, not the nice color transition. Tested as: tab.setStyle(“chromeColor”, [color, "white"]); …how to correct this?
Is there a way to set individual tab colors with the Spark TabBar ? The getChildAt(idx) returns a skin in the Spark TabBar.
same problem for me, any ideas? i’ve googled for a week now…. the only solutions are similar to this, but nothing about TabNavigator and tabBar, the Flash Builder 4.5 even doesn’t list mx.controls.tabBarClasses.Tab among context help…
Really Thanks in advance.
Found a sort of solution finally:
if you have:
and the following AS code:
protected function click_but2(event:MouseEvent):void
{
var box:NavigatorContent = new NavigatorContent;
box.label = "Child " + navigator.numChildren;
box.addEventListener(FlexEvent.CREATION_COMPLETE,tabCreationComplete);
navigator.addChild(box);
}
private function tabCreationComplete(event:FlexEvent):void{
navigator.getTabAt(navigator.numChildren -1).styleName="myTabCSSSelector";
}
and the following style section
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.myTabCSSSelector{
color:Yellow;
chromeColor:Navy;
}
this works BUT, if you click a second time the button the previuously created tab reverts its styleName property to the default “ButtonBarButton” value.
Debugging a bit deeper it seems that after the creation complete of the new tab and after the addChild method there is a re-drawing of the “hidden” tabBar of the TabNavigator which reverts every tab to its default…..
If some tester (better then me) can find out which is the problem maybe we could find a solution.
Thanks
half of my post has been cut off….
i’ll try again (a preview would be great….)
Found a sort of solution finally:
if you have:
and the following AS code:
protected function click_but2(event:MouseEvent):void { var box:NavigatorContent = new NavigatorContent; box.label = "Child " + navigator.numChildren; box.addEventListener(FlexEvent.CREATION_COMPLETE,tabCreationComplete); navigator.addChild(box); } private function tabCreationComplete(event:FlexEvent):void{ navigator.getTabAt(navigator.numChildren -1).styleName="myTabCSSSelector"; }and the following style section
@namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; .myTabCSSSelector{ color:Yellow; chromeColor:Navy; }this works BUT, if you click a second time the button the previuously created tab reverts its styleName property to the default “ButtonBarButton” value.
Debugging a bit deeper it seems that after the creation complete of the new tab and after the addChild method there is a re-drawing of the “hidden” tabBar of the TabNavigator which reverts every tab to its default…..
If some tester (better then me) can find out which is the problem maybe we could find a solution.
Thanks