The following example creates a ToggleButtonBar control in Flex and sets its toggleOnClick property to true.

According to the Flex 2.0.1 documentation:

If you set the toggleOnClick property of the ToggleButtonBar container to true, selecting the currently selected button deselects it. By default the toggleOnClick property is set to false.

You can check out the different behaviors of the toggleOnClick property by selecting or deselecting the check box in the upper left corner. Likewise, you can change the ToggleButtonBar control’s direction property by changing the selected value in the combo box in the upper-right corner. If the toggleOnClick property is set to true and you deselect a button in the ToggleButtonBar control, the ToggleButtonBar control’s selectedIndex property is set to -1 and the Flex application sets the ViewStack container’s visible property to false.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/22/creating-a-vertical-togglebuttonbar-in-flex/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="checkBox"
                label="toggleOnClick"
                selected="true" />

        <mx:Label text="selectedIndex: {toggleButtonBar.selectedIndex}"
                textAlign="center"
                width="100%" />

        <mx:Label text="direction:" />
        <mx:ComboBox id="comboBox"
                change="toggleButtonBar.direction = comboBox.selectedLabel">
            <mx:dataProvider>
                <mx:Array>
                    <mx:Object label="horizontal" />
                    <mx:Object label="vertical" />
                </mx:Array>
            </mx:dataProvider>
        </mx:ComboBox>
    </mx:ApplicationControlBar>

    <mx:HBox>
        <mx:ToggleButtonBar id="toggleButtonBar"
                dataProvider="{viewStack}"
                toggleOnClick="{checkBox.selected}" />

        <mx:ViewStack id="viewStack"
                width="160"
                height="120"
                visible="{toggleButtonBar.selectedIndex > -1}">
            <mx:Canvas label="Red" backgroundColor="red" />
            <mx:Canvas label="Orange" backgroundColor="haloOrange" />
            <mx:Canvas label="Yellow" backgroundColor="yellow" />
            <mx:Canvas label="Green" backgroundColor="haloGreen" />
            <mx:Canvas label="Blue" backgroundColor="haloBlue" />
        </mx:ViewStack>
    </mx:HBox>

</mx:Application>

View source is enabled in the following example.

 
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.

2 Responses to Creating a vertical ToggleButtonBar in Flex

  1. daniel says:

    Hey,
    i dont found any other way to contact you.
    I have a question.
    Is it posible that write a blog entry about sending data from Flex to AMFPHP with GET and POST using the RemoteObject?

    Thanks.

  2. Tyler Wright says:

    Daniel – Rob Taylor just wrote an excellent article on just that. See his post on using AMF via GET and POST.

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