Looping through buttons in a Spark ButtonBar control in Flex 4

The following example shows how you can loop through buttons in a Spark ButtonBar control in Flex 4 by setting the arrowKeysWrapFocus property.

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 www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see 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/04/22/looping-through-buttons-in-a-spark-buttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_NumericStepper_arrowKeysWrapFocus_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">
 
    <mx:Form>
        <mx:FormItem label="arrowKeysWrapFocus:">
            <s:CheckBox id="checkBox" />
        </mx:FormItem>
    </mx:Form>
 
    <s:ButtonBar id="btnBar"
            arrowKeysWrapFocus="{checkBox.selected}"
            horizontalCenter="0"
            verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList source="[Red,Orange,Yellow,Green,Blue]" />
        </s:dataProvider>
    </s:ButtonBar>
 
</s:Application>

View source is enabled in the following example.

You can also set the arrowKeysWrapFocus property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/22/looping-through-buttons-in-a-spark-buttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_NumericStepper_arrowKeysWrapFocus_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[
            protected function checkBox_change(evt:Event):void {
                btnBar.arrowKeysWrapFocus = checkBox.selected;
            }
        ]]>
    </fx:Script>
 
    <mx:Form>
        <mx:FormItem label="arrowKeysWrapFocus:">
            <s:CheckBox id="checkBox"
                    change="checkBox_change(event);" />
        </mx:FormItem>
    </mx:Form>
 
    <s:ButtonBar id="btnBar"
            horizontalCenter="0"
            verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList source="[Red,Orange,Yellow,Green,Blue]" />
        </s:dataProvider>
    </s:ButtonBar>
 
</s:Application>

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/04/22/looping-through-buttons-in-a-spark-buttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_NumericStepper_arrowKeysWrapFocus_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.collections.ArrayList;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import spark.components.ButtonBar;
            import spark.components.CheckBox;
 
            private var checkBox:CheckBox;
            private var btnBar:ButtonBar
 
            private function init():void {
                checkBox = new CheckBox();
                checkBox.addEventListener(Event.CHANGE, checkBox_change);
 
                var formItem:FormItem = new FormItem();
                formItem.label = "arrowKeysWrapFocus:";
                formItem.addChild(checkBox);
 
                var form:Form = new Form();
                form.addChild(formItem);
                addElement(form);
 
                var arr:Array = ["Red", "Orange", "Yellow", "Green", "Blue"];
 
                btnBar = new ButtonBar();
                btnBar.horizontalCenter = 0;
                btnBar.verticalCenter = 0;
                btnBar.dataProvider = new ArrayList(arr);
                addElement(btnBar);
            }
 
            protected function checkBox_change(evt:Event):void {
                btnBar.arrowKeysWrapFocus = checkBox.selected;
            }
        ]]>
    </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.