Setting the base color on a Spark CheckBox control in Flex 4

In a previous example, “Setting the icon color on a Spark CheckBox control in Flex 4”, we saw how you can set the icon (check mark) color on a Spark CheckBox control in Flex 4 by setting the symbolColor style.

The following example shows how you can set the base (background) color on a Spark CheckBox control in Flex 4 by setting the baseColor style.

Full code after the jump.

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/30/setting-the-base-color-on-an-fxcheckbox-control-in-flex-gumbo/ -->
<s:Application name="Spark_CheckBox_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:CheckBox id="checkBox"
            label="Spark CheckBox"
            selected="true"
            baseColor="black"
            symbolColor="red"
            horizontalCenter="0"
            verticalCenter="0" />

</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:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/30/setting-the-base-color-on-an-fxcheckbox-control-in-flex-gumbo/ -->
<s:Application name="Spark_CheckBox_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 mx "library://ns.adobe.com/flex/halo";
        @namespace s "library://ns.adobe.com/flex/spark";

        s|CheckBox {
            baseColor: black;
            symbolColor: red;
        }
    </fx:Style>

    <s:CheckBox id="checkBox"
            label="Spark CheckBox"
            selected="true"
            horizontalCenter="0"
            verticalCenter="0" />

</s:Application>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/30/setting-the-base-color-on-an-fxcheckbox-control-in-flex-gumbo/ -->
<s:Application name="Spark_CheckBox_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:Script>
        <![CDATA[
            private function btn_click(evt:Event):void {
                checkBox.setStyle("baseColor", "black");
                checkBox.setStyle("symbolColor", "red");
            }
        ]]>
    </fx:Script>

    <s:Button id="btn"
            label="Set base color"
            click="btn_click(event);"
            x="10"
            y="10" />

    <s:CheckBox id="checkBox"
            label="Spark CheckBox"
            selected="true"
            horizontalCenter="0"
            verticalCenter="0" />

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

One thought on “Setting the base color on a Spark CheckBox control in Flex 4

  1. Great Example: direct, simple and very usefull.

    Showing the true real power of Flex SDK an unexplorabled framework for multiple enterprises, in special Latinamerica

Comments are closed.