Styling the decrement button and increment button on a Spark Spinner control in Flex 4

The following example shows how you can use advanced CSS to style the decrement and decrement buttons on a Spark Spinner control’s vertical scroll bar in Flex 4 by styling the #decrementButton and #incrementButton selectors.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex 4 SDK installed in your Flex Builder 3. For more information on downloading and installing the Flex 4 SDK into Flex Builder 3, see “Using the beta Flex 4 SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/27/styling-the-decrement-button-and-increment-button-on-an-fxspinner-control-in-flex-gumbo/ -->
<s:Application name="Spark_Spinner_decrementButton_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 mx "library://ns.adobe.com/flex/halo";
        @namespace s "library://ns.adobe.com/flex/spark";

        s|Spinner #incrementButton {
            baseColor: haloGreen;
        }
        s|Spinner #decrementButton {
            baseColor: red;
        }
    </fx:Style>

    <s:HGroup id="group"
            horizontalCenter="0"
            verticalCenter="0">
        <s:SimpleText id="simpleTxt"
                text="{spinner.value}"
                textAlign="center"
                verticalAlign="middle"
                width="30"
                height="100%"  />
        <s:Spinner id="spinner" />
    </s:HGroup>

</s:Application>

View source is enabled in the following example.

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/27/styling-the-decrement-button-and-increment-button-on-an-fxspinner-control-in-flex-gumbo/ -->
<s:Application name="Spark_Spinner_decrementButton_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 mx.events.FlexEvent;
            import spark.components.HGroup;
            import flashx.textLayout.formats.VerticalAlign;
            import flashx.textLayout.formats.TextAlign;
            import spark.components.Spinner;
            import spark.primitives.SimpleText;

            private var simpleTxt:SimpleText;
            private var spinner:Spinner;

            private function init():void {
                simpleTxt = new SimpleText();
                simpleTxt.text = "0";
                simpleTxt.setStyle("textAlign", TextAlign.CENTER);
                simpleTxt.setStyle("verticalAlign", VerticalAlign.MIDDLE);
                simpleTxt.width = 30;
                simpleTxt.percentHeight = 100;

                spinner = new Spinner();
                spinner.addEventListener(FlexEvent.INITIALIZE, spinner_init);
                spinner.addEventListener(Event.CHANGE, spinner_change);

                var group:HGroup = new HGroup();
                group.horizontalCenter = 0;
                group.verticalCenter = 0;
                group.addElement(simpleTxt);
                group.addElement(spinner);
                addElement(group);
            }

            private function spinner_init(evt:FlexEvent):void {
                spinner.incrementButton.setStyle("baseColor", "haloGreen");
                spinner.decrementButton.setStyle("baseColor", "red");
            }

            private function spinner_change(evt:Event):void {
                simpleTxt.text = spinner.value.toString();
            }
        ]]>
    </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.