Setting horizontal and vertical gaps between buttons in a Flex ButtonBar control

The following example shows how you can control the horizontal and vertical gaps between buttons in a ButtonBar control in Flex by setting the verticalGap and horizontalGap styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/08/setting-horizontal-and-vertical-gaps-between-buttons-in-a-flex-buttonbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="The" />
        <mx:Object label="quick" />
        <mx:Object label="brown" />
        <mx:Object label="fox" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="verticalGap:" direction="horizontal">
                <mx:HSlider id="slider"
                        minimum="-2"
                        maximum="10"
                        value="0"
                        liveDragging="true"
                        snapInterval="1"
                        tickInterval="1" />
                <mx:Label text="{slider.value}" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ButtonBar id="buttonBar"
            dataProvider="{arr}"
            verticalGap="{slider.value}"
            direction="vertical" />

</mx:Application>

View source is enabled in the following example.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/08/setting-horizontal-and-vertical-gaps-between-buttons-in-a-flex-buttonbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="The" />
        <mx:Object label="quick" />
        <mx:Object label="brown" />
        <mx:Object label="fox" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalGap:" direction="horizontal">
                <mx:HSlider id="slider"
                        minimum="-2"
                        maximum="10"
                        value="0"
                        liveDragging="true"
                        snapInterval="1"
                        tickInterval="1" />
                <mx:Label text="{slider.value}" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ButtonBar id="buttonBar"
            dataProvider="{arr}"
            horizontalGap="{slider.value}"
            direction="horizontal" />

</mx:Application>

View source is enabled in the following example.

You could also set the horizontalGap or verticalGap style in an external .CSS file or <mx:Style /> block, as seen in the following snippet:

<mx:Style>
    ButtonBar {
        horizontalGap: 16;
    }
</mx:Style>

Or, you can set the horizontalGap or verticalGap style using ActionScript, as seen in the following snippet:

<mx:Script>
    <![CDATA[
        private function init():void {
            buttonBar.setStyle("horizontalGap", 16);
        }
    ]]>
</mx:Script>

7 thoughts on “Setting horizontal and vertical gaps between buttons in a Flex ButtonBar control

  1. Just spent a few hours trying to adjust the padding of the individual buttons in a button bar inline in actionscript. This works, but it ain’t that pretty:

    var buttonStyleObj:CSSStyleDeclaration = new CSSStyleDeclaration("buttonStyleObj");
    buttonStyleObj.setStyle("paddingRight", 4);
    buttonStyleObj.setStyle("paddingLeft", 4);
    StyleManager.setStyleDeclaration(".buttonStyle", buttonStyleObj, true);
    
    var buttonBar:ButtonBar = new ButtonBar();
    buttonBar.setStyle("buttonStyleName", "buttonStyle");
    
    1. Thank you for posting your solution, I was searching for that exact thing. I ended up translating your code into a style declaration and applying it to my button bar:

      1
      2
      3
      4
      5
      
       
        .buttonStyle { paddingLeft: 4; paddingRight: 4; }
       
       
      ...
      1. My kingdom for a preview button!

        1
        2
        
         
          .buttonStyle { paddingLeft: 4; paddingRight: 4; }
  2. Oh well…code is not going to show properly. All I did is create a style definition and set the buttonStyleName of the button bar to the style.

Comments are closed.