Setting the base color on the Spark HScrollBar control in Flex 4

The following example shows how you can style the base color on the Flex 4 Spark HScrollBar control’s thumb, track, increment button, and/or decrement button by setting the baseColor style.

Full code after the jump.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/14/setting-the-base-color-on-the-fxhscrollbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_HScrollBar_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="baseColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="#CCCCCC" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:HScrollBar id="scrollBar"
                baseColor="{colorPicker.selectedColor}" />
    </s:VGroup>
 
</s:Application>

View source is enabled in the following example.

You can also set the baseColor style in an external .CSS file or <Style/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/14/setting-the-base-color-on-the-fxhscrollbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_HScrollBar_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
 
        s|HScrollBar {
            baseColor: haloBlue;
        }
    </fx:Style>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:HScrollBar id="scrollBar" />
    </s:VGroup>
 
</s:Application>

Or, you can set the baseColor style using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/14/setting-the-base-color-on-the-fxhscrollbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_HScrollBar_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="baseColor:">
                <mx:ColorPicker id="colorPicker"
                        selectedColor="#CCCCCC"
                        change="colorPicker_change(event);"/>
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import mx.events.ColorPickerEvent;
 
            protected function colorPicker_change(evt:ColorPickerEvent):void {
               scrollBar.setStyle("baseColor", evt.color);
            }
        ]]>
    </fx:Script>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:HScrollBar id="scrollBar" />
    </s:VGroup>
 
</s:Application>

Finally, if you wanted to set the baseColor style on the Spark HScrollBar thumb, track, increment button, and/or decrement button individually, you could either create a custom Spark HScrollBar skin and set the baseColor styles in the new skin, or you could style the #thumb, #track, #incrementButton, and/or #decrementButton selectors directly, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/14/setting-the-base-color-on-the-fxhscrollbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_HScrollBar_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo">
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
 
        s|HScrollBar #decrementButton {
            baseColor: red;
        }
        s|HScrollBar #incrementButton {
            baseColor: haloGreen;
        }
        s|HScrollBar #track {
            baseColor: haloBlue;
        }
        s|HScrollBar #thumb {
            baseColor: haloOrange;
        }
    </fx:Style>
 
    <s:VGroup horizontalCenter="0" verticalCenter="0">
        <s:HScrollBar id="scrollBar" />
    </s:VGroup>
 
</s:Application>

View source is enabled in the following example.

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/14/setting-the-base-color-on-the-fxhscrollbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_HScrollBar_baseColor_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/halo"
        initialize="init();">
 
    <fx:Script>
        <![CDATA[
            import spark.components.HScrollBar;
            import spark.components.VGroup;
 
            private const scrollBar:HScrollBar = new HScrollBar();
 
            private function init():void {
                var cssObj:CSSStyleDeclaration = StyleManager.getStyleDeclaration("spark.components.HScrollBar");
                cssObj.setStyle("baseColor", "haloBlue");
 
                var vGr:VGroup = new VGroup();
                vGr.horizontalCenter = 0;
                vGr.verticalCenter = 0;
                vGr.addElement(scrollBar);
                addElement(vGr);
            }
        ]]>
    </fx:Script>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

5 thoughts on “Setting the base color on the Spark HScrollBar control in Flex 4

  1. Hi, baseColor its not in the final release of Flex 4, how can I change the colors of the scroll? Ive been seraching and trying to skin the scroll bar but so far i couldnt… :(

  2. Thanks for your advice, but im getting a “The style ‘chromeColor’ is only supported by type ‘spark.components.Button’ with the theme(s) ‘spark’.” error.
    Im using a Halo theme by the way…

    1. @GuitarWorker,

      Ah, I never use the old Halo theme with Flex 4 apps, but this may work for you (it worked for me in Flex 4.1, but gave me a warning):

       
      <?xml version="1.0" encoding="utf-8"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark">
       
          <fx:Style>
              @namespace s "library://ns.adobe.com/flex/spark";
       
              s|Button {
                  chromeColor: red;
              }
          </fx:Style>
       
          <s:Button id="btn" label="foo" click="btn.setStyle('chromeColor', 'haloOrange');" />
       
      </s:Application>

      Peter

      1. OK, the error was because I was using chromeColor in the MXML of the skin:

        But youre right, via CSS theres just a warning but it works!

        s|VScrollBar #track {
        chromeColor: #000000;
        }

        Thanks Peter!

Comments are closed.