Determining when the selected state of a Spark ToggleButton control changes in Flex 4

by Peter deHaan on February 6, 2009

in ToggleButton (Spark), beta1

The following example shows how you can determine when the Flex 4 Spark ToggleButton control is selected or deselected by listening for the change event and checking the Boolean selected 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/02/06/determining-when-the-selected-state-of-a-fxtogglebutton-control-changes-in-flex-gumbo/ -->
<s:Application name="Spark_ToggleButton_change_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[
            import mx.controls.dataGridClasses.DataGridColumn;
 
            private function btn_change(evt:Event):void {
                var obj:Object = {};
                obj.selected = btn.selected;
                obj.time = new Date();
 
                arrColl.addItemAt(obj, 0);
                if (arrColl.length > dataGrid.rowCount) {
                    arrColl.removeItemAt(dataGrid.rowCount);
                }
            }
 
            private function time_labelFunc(item:Object, col:DataGridColumn):String {
                return item.time.toTimeString();
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:ArrayCollection id="arrColl" />
    </fx:Declarations>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <s:ToggleButton id="btn"
                label="{btn.selected}"
                baseColor="haloBlue"
                change="btn_change(event);" />
    </mx:ApplicationControlBar>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{arrColl}"
            draggableColumns="false"
            width="300"
            horizontalCenter="0" verticalCenter="0">
        <mx:columns>
            <mx:DataGridColumn dataField="selected" />
            <mx:DataGridColumn dataField="time"
                    labelFunction="time_labelFunc" />
        </mx:columns>
    </mx:DataGrid>
 
</s:Application>

View source is enabled in the following example.

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.

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

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

Previous post:

Next post: