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

by Peter deHaan on March 14, 2009

in HScrollBar (Spark), beta2

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 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".

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

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: