Creating a vertical Spark ButtonBar control in Flex 4

The following example shows how you can create a vertical Spark ButtonBar control in Flex 4 by setting the layout property to a VerticalLayout object and setting the gap property to -1.

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.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/13/creating-a-vertical-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_ButtonBar_layout_VerticalLayout_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">

    <s:ButtonBar id="btnBar"
            horizontalCenter="0"
            verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="-1" />
        </s:layout>
        <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 button layout direction by creating a custom skin and setting the skinClass style, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/13/creating-a-vertical-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_ButtonBar_layout_VerticalLayout_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">

    <s:ButtonBar id="btnBar"
            horizontalCenter="0"
            verticalCenter="0"
            skinClass="skins.CustomButtonBarSkin">
        <s:dataProvider>
            <s:ArrayList source="[Red,Orange,Yellow,Green,Blue]" />
        </s:dataProvider>
    </s:ButtonBar>

</s:Application>

The custom MXML skin, skins/CustomButtonBarSkin.mxml, is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/13/creating-a-vertical-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Skin name="CustomButtonBarSkin"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        alpha.disabled="0.5">
    <s:states>
        <s:State name="normal" />
        <s:State name="disabled" />
    </s:states>

    <fx:Metadata>
        <![CDATA[
            [HostComponent("spark.components.ButtonBar")]
        ]]>
    </fx:Metadata>

    <fx:Declarations>
        <fx:Component id="middleButton" >
            <s:ButtonBarButton skinClass="spark.skins.default.ButtonBarMiddleButtonSkin" />
        </fx:Component>
    </fx:Declarations>

    <s:DataGroup id="dataGroup" width="100%" height="100%">
        <s:layout>
            <s:VerticalLayout gap="-1"/>
        </s:layout>
    </s:DataGroup>

</s:Skin>

You can also set the skinClass style in an external .CSS file or <Style/> block, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/13/creating-a-vertical-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_ButtonBar_layout_VerticalLayout_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:Style>
        @namespace s "library://ns.adobe.com/flex/spark";

        s|ButtonBar {
            skinClass: ClassReference("skins.CustomButtonBarSkin");
        }
    </fx:Style>

    <s:ButtonBar id="btnBar"
            horizontalCenter="0"
            verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList source="[Red,Orange,Yellow,Green,Blue]" />
        </s:dataProvider>
    </s:ButtonBar>

</s:Application>

Or, you can set the skinClass style using ActionScript, as seen in the following example:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/13/creating-a-vertical-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_ButtonBar_layout_VerticalLayout_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"
        backgroundColor="white">

    <fx:Script>
        <![CDATA[
            import skins.CustomButtonBarSkin;

            private function btn_click(evt:MouseEvent):void {
                btnBar.setStyle("skinClass", CustomButtonBarSkin);
            }
        ]]>
    </fx:Script>

    <s:Button id="btn"
            label="Set skin class"
            click="btn_click(event);"
            x="10"
            y="10" />

    <s:ButtonBar id="btnBar"
            horizontalCenter="0"
            verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList source="[Red,Orange,Yellow,Green,Blue]" />
        </s:dataProvider>
    </s:ButtonBar>

</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.