Creating a toggle button in Flex using the FxToggleButton control

The following example shows how you can create a toggle button in Flex Gumbo using the FxToggleButton control in the latest version (build 4.0.0.3584) of the beta Flex Gumbo SDK.
This example also shows the new Flex Gumbo syntax for two-way data binding:

  • inline: <FxToggleButton id="fxToggleButton" selected="@{fxCheckBox.selected}" />
  • MXML: <Binding source="fxCheckBox.selected" destination="fxToggleButton.selected" twoWay="true" />

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/2008/10/13/creating-a-toggle-button-in-flex-using-the-fxtogglebutton-control/ -->
<FxApplication name="FxToggleButton_test"
        xmlns="http://ns.adobe.com/mxml/2009">
    <layout>
        <BasicLayout />
    </layout>

    <VGroup id="vGroup" left="10" top="10">
        <FxCheckBox id="fxCheckBox"
                label="selected" />
    </VGroup>

    <FxToggleButton id="fxToggleButton"
            label="FxToggleButton"
            selected="@{fxCheckBox.selected}"
            horizontalCenter="0"
            verticalCenter="0" />

</FxApplication>

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/2008/10/13/creating-a-toggle-button-in-flex-using-the-fxtogglebutton-control/ -->
<FxApplication name="FxToggleButton_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        initialize="init();">
    <layout>
        <BasicLayout />
    </layout>

    <Script>
        <![CDATA[
            import mx.components.FxCheckBox;
            import mx.components.FxToggleButton;
            import mx.components.VGroup;

            private var fxCheckBox:FxCheckBox;
            private var fxToggleButton:FxToggleButton;
            private var vGroup:VGroup;

            private function init():void {
                fxCheckBox = new FxCheckBox();
                fxCheckBox.label = "selected";
                fxCheckBox.addEventListener(Event.CHANGE,
                            fxCheckBox_change);

                vGroup = new VGroup();
                vGroup.setStyle("left", 10);
                vGroup.setStyle("top", 10);
                vGroup.addItem(fxCheckBox);
                addItem(vGroup);

                fxToggleButton = new FxToggleButton();
                fxToggleButton.label = "FxToggleButton";
                fxToggleButton.selected = fxCheckBox.selected;
                fxToggleButton.setStyle("horizontalCenter", 0);
                fxToggleButton.setStyle("verticalCenter", 0);
                fxToggleButton.addEventListener(Event.CHANGE,
                            fxToggleButton_change);
                addItem(fxToggleButton);
            }

            private function fxCheckBox_change(evt:Event):void {
                fxToggleButton.selected = fxCheckBox.selected;
            }

            private function fxToggleButton_change(evt:Event):void {
                fxCheckBox.selected = fxToggleButton.selected;
            }
        ]]>
    </Script>

</FxApplication>

Or, If you wanted to create a two-way binding in MXML instead of using event listeners, you could use the following code:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/10/13/creating-a-toggle-button-in-flex-using-the-fxtogglebutton-control/ -->
<FxApplication name="FxToggleButton_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        initialize="init();">
    <layout>
        <BasicLayout />
    </layout>

    <Binding source="fxCheckBox.selected"
            destination="fxToggleButton.selected"
            twoWay="true" />

    <Script>
        <![CDATA[
            import mx.components.FxCheckBox;
            import mx.components.FxToggleButton;
            import mx.components.VGroup;

            [Bindable]
            private var fxCheckBox:FxCheckBox;
            [Bindable]
            private var fxToggleButton:FxToggleButton;
            private var vGroup:VGroup;

            private function init():void {
                fxCheckBox = new FxCheckBox();
                fxCheckBox.label = "selected";

                vGroup = new VGroup();
                vGroup.setStyle("left", 10);
                vGroup.setStyle("top", 10);
                vGroup.addItem(fxCheckBox);
                addItem(vGroup);

                fxToggleButton = new FxToggleButton();
                fxToggleButton.label = "FxToggleButton";
                fxToggleButton.selected = fxCheckBox.selected;
                fxToggleButton.setStyle("horizontalCenter", 0);
                fxToggleButton.setStyle("verticalCenter", 0);
                addItem(fxToggleButton);
            }
        ]]>
    </Script>

</FxApplication>

For more information on two-way data binding, see the “Two-way Data Binding – Functional and Design Specification” document on Gumbo page on opensource.adobe.com.

This entry is based on a beta version of the Flex Gumbo 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 Gumbo SDK.

2 thoughts on “Creating a toggle button in Flex using the FxToggleButton control

  1. Wonderful Demo~
    Thank you very much.
    By the way, could you tell me how to config FlexBuilder 3.0 to write Gumbo Application like you do?

    The boring thing is no hint about Gumbo, could it be solved?

    Thanks again~
    mani.

  2. mani,

    For instructions on using the Gumbo SDK in Flex Builder 3, check out “Using the beta Gumbo SDK in Flex Builder 3″ (I also try and post this link at the top of every Gumbo/beta entry on the blog).

    The example above is fairly recent and should work in the latest beta Flex Gumbo SDK nightly builds, but some earlier beta Gumbo blog entries are probably somewhat “broken” due to various API naming changes, namespace changes, etc. So your mileage may vary.

    I believe code hinting should work in the latest Gumbo SDKs and Flex Builder 3. I use Flex Builder 3.0.1 and nightly Gumbo builds at home and work and for the most part it’s pretty smooth.

    Peter

Comments are closed.