The following example shows how you can style the various states on a Flex 4 Spark CheckBox control using a <Style> block.

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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/17/styling-the-different-states-on-an-fxcheckbox-control-in-flex-gumbo/ -->
<s:Application name="FxCheckBox_statesStyle_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:layout>
        <s:BasicLayout />
    </s:layout>
 
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
 
        /* all states */
        s|CheckBox {
            fontWeight: bold;
            fontSize: 24;
            symbolColor: black;
        }
 
        /* deselected states */
        s|CheckBox:up,
        s|CheckBox:over,
        s|CheckBox:down,
        s|CheckBox:disabled {
            baseColor: red;
            color: red;
        }
 
        /* selected states */
        s|CheckBox:upAndSelected,
        s|CheckBox:overAndSelected,
        s|CheckBox:downAndSelected,
        s|CheckBox:disabledAndSelected {
            baseColor: green;
            color: green;
        }
    </fx:Style>
 
    <s:CheckBox id="ch"
                label="enabled"
                selected="true"
                color="black"
                baseColor="white"
                x="10"
                y="10" />
 
    <s:CheckBox id="checkBox"
                label="FxCheckBox"
                enabled="{ch.selected}"
                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.

 
Tagged with:
 
About The Author

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.

5 Responses to Styling the different states on a Spark CheckBox control in Flex 4

  1. Richard says:

    I must be missing something, you show the working .swf somewhere, right?

  2. Peter deHaan says:

    Richard,

    Nope, due to limited time lately I haven’t been posting SWFs. But these are pretty easy to copy/paste into Flex Builder and compile using the latest Gumbo SDK.

    Peter

  3. Jian(China) says:

    Hi, I compile the code on http://blog.flexexamples.com/2009/02/11/styling-specific-states-in-an-fxtogglebutton-control-in-flex-gumbo/ by flex 4(Gumbo), and the compiler does not report any error, but in Flash Player 10, the selected states style has no effect, could you help me? Thank you.

  4. Peter deHaan says:

    Jian(China),

    I’m not sure which version of the Gumbo SDK you’re using, but try downloading the latest version from http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4 and see if that helps.

    Peter

  5. Jian(China) says:

    Peter,

    Thank you for your reply. I download the latest stable build(4.0.0.4904 ), and it works.thx.

Leave a Reply

Your email address will not be published.

You may 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