Creating a tile layout Spark ButtonBar control in Flex 4

In a previous example, “Creating a vertical Spark ButtonBar control in Flex 4”, we saw how you can create a vertical Spark ButtonBar control in Flex 4 by setting the layout property to a VerticalLayout object.

The following example shows how you can create a tile layout on the Spark ButtonBar control in Flex 4 by setting the layout property to a TileLayout object.

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/03/26/creating-a-tile-layout-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_ButtonBar_TileLayout_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="[The,Quick,Brown,Fox,Jumps,Over,The,Lazy,Dog]" />
        </s:dataProvider>
    </s:ButtonBar>

</s:Application>

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/26/creating-a-tile-layout-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:TileLayout requestedColumnCount="3"
                    columnWidth="200"
                    horizontalGap="-1"
                    verticalGap="-1" />
        </s:layout>
    </s:DataGroup>

</s:Skin>

View source is enabled in the following example.

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/03/26/creating-a-tile-layout-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_ButtonBar_TileLayout_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="[The,Quick,Brown,Fox,Jumps,Over,The,Lazy,Dog]" />
        </s:dataProvider>
    </s:ButtonBar>

</s:Application>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/03/26/creating-a-tile-layout-fxbuttonbar-control-in-flex-gumbo/ -->
<s:Application name="Spark_ButtonBar_TileLayout_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 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);"
            left="10"
            top="10" />

    <s:ButtonBar id="btnBar"
            horizontalCenter="0"
            verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList source="[The,Quick,Brown,Fox,Jumps,Over,The,Lazy,Dog]" />
        </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.

One thought on “Creating a tile layout Spark ButtonBar control in Flex 4

  1. Extremely helpful, thank you! Should note that that “spark.skins.default.ButtonBarMiddleButtonSkin” is now “spark.skins.spark.ButtonBarMiddleButtonSkin” in Flash Builder 4.5

Comments are closed.