Changing the fill colors of a ButtonBar control in Flex
The following example shows how you can change the background fill colors of a ButtonBar control in Flex by setting the buttonStyleName, fillColors, and fillAlphas styles.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/02/changing-the-fill-colors-of-a-buttonbar-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
.allButtons2 {
/* Creates a gradient from red/orange when button is in the
"out" state. */
fillColors: red, haloOrange;
}
.allButtons4 {
/* Creates a gradient from red/orange when button is in the
"out" state. Creates a gradient from green/blue when
button is in the "over" state. */
fillColors: red, haloOrange, haloGreen, haloBlue;
}
.allButtonsSolid {
fillColors: red, red;
}
.allButtonsSolidOpaque {
fillAlphas: 1.0, 1.0;
fillColors: red, red;
}
</mx:Style>
<mx:ViewStack id="viewStack"
visible="false"
includeInLayout="false">
<mx:VBox label="One">
<mx:Label text="One" />
</mx:VBox>
<mx:VBox label="Two">
<mx:Label text="Two" />
</mx:VBox>
<mx:VBox label="Three">
<mx:Label text="Three" />
</mx:VBox>
</mx:ViewStack>
<mx:Form>
<mx:FormItem label="2 fill colors defined:">
<mx:ButtonBar id="buttonBar2"
buttonStyleName="allButtons2"
dataProvider="{viewStack}" />
</mx:FormItem>
<mx:FormItem label="4 fill colors defined:">
<mx:ButtonBar id="buttonBar4"
buttonStyleName="allButtons4"
dataProvider="{viewStack}" />
</mx:FormItem>
<mx:FormItem label="solid fill:">
<mx:ButtonBar id="buttonBarSolid"
buttonStyleName="allButtonsSolid"
dataProvider="{viewStack}" />
</mx:FormItem>
<mx:FormItem label="solid opaque fill:">
<mx:ButtonBar id="buttonBarSolidOpaque"
buttonStyleName="allButtonsSolidOpaque"
dataProvider="{viewStack}" />
</mx:FormItem>
</mx:Form>
</mx:Application>
View source is enabled in the following example.
As you can see in the previous example, there are four separate ButtonBar controls:
1) The first ButtonBar control, buttonBar2, uses a custom buttonStyleName style (.allButtons2) which has an array of two items defined for its fillColors array:
.allButtons2 {
/* Creates a gradient from red/orange when button is in the
"out" state. */
fillColors: red, haloOrange;
}
These two colors specify the gradient that is used for each button in the ButtonBar control. As you can see, the top of the button has a red-ish hue and the bottom of the button has more of an orange-ish hue.
2) The second ButtonBar control, buttonBar4, uses a custom buttonStyleName style (.allButtons4) which has an array of four items defined for its fillColors array:
.allButtons4 {
/* Creates a gradient from red/orange when button is in the
"out" state. Creates a gradient from green/blue when
button is in the "over" state. */
fillColors: red, haloOrange, haloGreen, haloBlue;
}
The first two colors specify the gradient that is used for the “out” state (the state where the cursor is not over the button) of each button. The last two colors specify the gradient that is used for the “over” state (the state where the cursor is over the button) of each button. So again, when the cursor is not over the button, the top of the button has a red-ish hue and the bottom of the button has more of an orange-ish hue. When the user moves their cursor over a button in the ButtonBar control, the button switches to a green-to-blue gradient.
3) The third ButtonBar control, buttonBarSolid, uses a custom buttonStyleName style (.allButtonsSolid) which has an array of two items defined for its fillColors array:
.allButtonsSolid {
fillColors: red, red;
}
Both of the colors are the same, giving each of the buttons a solid fill instead of a gradient fill. Similar to the first ButtonBar control, buttonBar2, moving the mouse cursor over each button in the ButtonBar control a slightly darker looking fill.
4) The fourth ButtonBar control, buttonBarSolidOpaque, uses a custom buttonStyleName style (.allButtonsSolidOpaque) which has an array of two items defined for its fillColors array, but it also defines a new style, fillAlphas, which is an array of alpha fill values:
.allButtonsSolidOpaque {
fillAlphas: 1.0, 1.0;
fillColors: red, red;
}
As you can see, the fill colors are again using the same color for each entry, giving the button fill a solid fill instead of a gradient fill. The fillAlphas style is an array of two values, [1.0, 1.0], which controls the alpha for each of the items in the fillColors style array. This causes the fill color to appear as a deeper red, rather than a slightly faded/muted appearance.
You can also set the fillColors and fillAlphas styles using ActionScript, as seen in the following example:
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/02/changing-the-fill-colors-of-a-buttonbar-control-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white">
<mx:Style>
.customButtonBarFillColors {
}
</mx:Style>
<mx:Script>
<![CDATA[
private function init():void {
var cssObj:CSSStyleDeclaration = StyleManager.getStyleDeclaration(".customButtonBarFillColors");
cssObj.setStyle("fillColors", [0xFF0000, "haloOrange", "haloOrange", "#FF0000"]);
cssObj.setStyle("fillAlphas", [1.0, 1.0, 0.2, 0.4]);
}
]]>
</mx:Script>
<mx:ApplicationControlBar dock="true">
<mx:Button id="button"
label="set button styles"
click="init();" />
</mx:ApplicationControlBar>
<mx:ButtonBar id="buttonBar"
buttonStyleName="customButtonBarFillColors"
themeColor="haloOrange">
<mx:dataProvider>
<mx:Array>
<mx:String>One</mx:String>
<mx:String>Two</mx:String>
<mx:String>Three</mx:String>
</mx:Array>
</mx:dataProvider>
</mx:ButtonBar>
</mx:Application>
View source is enabled in the following example.
Note that you can also specify an additional two values for the fillAlphas style which represent the fill values when the current button in a ButtonBar control is in an “over” state, similar to the second ButtonBar control (buttonBar4) in the first example.
So in the previous example, when you roll over each button in the buttonBar ButtonBar control the fill alpha for the button goes from a solid 1.0/1.0 to a gradient fill alpha of 0.2/0.4 (20% alpha at the top of the button to 40% alpha at the bottom of the button).
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.
6 Responses to Changing the fill colors of a ButtonBar control in Flex
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


In regards to making the color solidOpaque why can’t the fillColors accept custom colors. Why does it have to be something generic like “red”, “ffffff”, “cccccc”. I tried entering 1.0, 1.0 and #346689, #346689… it doe snot come out solid.
Can the down state be styled?
Alternatively, I see that there is a style for downSkin? Can a downSkin be set to match upSkin?
Thanks,
g
You’re an asshole, this doesn’t work!
:(
S-E-R-I-O-U-S-L-Y ??
How does that work in Actionscript?