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 beta, check out the Adobe Flash Builder 4 page on the Adobe Labs site. To download the latest build of the Flex 4 SDK, see http://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4. For instructions on using the beta Flex 4 SDK in Flex Builder 3, see "Using the beta Flex 4 SDK in Flex Builder 3".
<?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:
<?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:
<?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.
