Accessing specific buttons in a Spark ButtonBar control in Flex 4

In a suspiciously similar previous example, “Disabling specific buttons in a Spark ButtonBar control in Flex 4”, we saw how you could disable specific buttons in a Spark ButtonBar control in Flex 4 by using a custom skin and setting the skinClass style.

The following example shows you how you can modify specific buttons at runtime using the Flex 4 Spark ButtonBar control’s dataGroup property and getElementAt() method.

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/08/02/accessing-specific-buttons-in-a-spark-buttonbar-control-in-flex-4/ -->
<s:Application name="Spark_ButtonBar_dataGroup_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 spark.components.ButtonBarButton;
 
            protected function btn1_click(evt:MouseEvent):void {
                var btnBarBtn:ButtonBarButton = btnBar.dataGroup.getElementAt(0) as ButtonBarButton;
                btnBarBtn.enabled = !btnBarBtn.enabled;
            }
        ]]>
    </fx:Script>
 
    <mx:ApplicationControlBar width="100%" cornerRadius="0">
        <s:Button id="btn1"
                label="Toggle button 1"
                click="btn1_click(event);" />
    </mx:ApplicationControlBar>
 
    <s:ButtonBar id="btnBar"
            requireSelection="true"
            horizontalCenter="0" verticalCenter="0">
        <s:dataProvider>
            <s:ArrayList source="[The quick brown,Fox jumps over,The lazy dog]" />
        </s:dataProvider>
    </s:ButtonBar>
 
</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.

7 thoughts on “Accessing specific buttons in a Spark ButtonBar control in Flex 4

  1. I really need some help. How do I access a specific button in a TabBar?
    tabbar.dataGroup.numChilderen displays 0.

  2. I’ve tried this code and I get a ButtonBarSkinInnerClass instead of a button kind class from the btnBar.dataGroup.getElementAt(0). That thing has changed in the last SDK? This doesn’t works with skinned buttonbar?.

    1. @Amatar,

      I just tested this with the latest Flex Hero nightly build and the same basic code still compiles for me;

      <?xml version="1.0" encoding="utf-8"?>
      <!-- http://blog.flexexamples.com/2009/08/02/accessing-specific-buttons-in-a-spark-buttonbar-control-in-flex-4/ -->
      <s:Application name="Spark_ButtonBar_dataGroup_test"
                     xmlns:fx="http://ns.adobe.com/mxml/2009"
                     xmlns:s="library://ns.adobe.com/flex/spark"
                     xmlns:mx="library://ns.adobe.com/flex/mx">
       
          <fx:Script>
              <![CDATA[
                  import spark.components.ButtonBarButton;
       
                  protected function btn1_click(evt:MouseEvent):void {
                      var btnBarBtn:ButtonBarButton = btnBar.dataGroup.getElementAt(0) as ButtonBarButton;
                      btnBarBtn.enabled = !btnBarBtn.enabled;
                  }
              ]]>
          </fx:Script>
       
          <s:controlBarContent>
              <s:Button id="btn1"
                        label="Toggle button 1"
                        click="btn1_click(event);" />
          </s:controlBarContent>
       
          <s:ButtonBar id="btnBar"
                       requireSelection="true"
                       horizontalCenter="0" verticalCenter="0">
              <s:dataProvider>
                  <s:ArrayList source="[The quick brown,Fox jumps over,The lazy dog]" />
              </s:dataProvider>
          </s:ButtonBar>
       
      </s:Application>

      Peter

      1. I’m also finding that it is not working for a skinned button bar. My code for dataGroup.getElementAt(0) returns a skin-class reference & throws a null pointer when I try to cast it to the actual componet

  3. @Amatar & Anonymous:

    No problem casting the skin-class reference to a custom skinnned ButtonBarButton here:


    override public function set enabled( value:Boolean ):void
    {
    if ( dataGroup )
    {
    var i:int = 0;
    var btn:ChatTabBarButton;
    var n:int = dataGroup.numElements;

    for ( i; i < n; i++ )
    {
    btn = dataGroup.getElementAt( i ) as ChatTabBarButton;
    trace(btn);
    }
    }

    super.enabled = value;
    }

Comments are closed.