Setting the horizontal gap between an icon and label on a LinkButton control in Flex

The following example shows how you can set the gap between the icon and label on a Flex LinkButton control by setting the horizontalGap style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/06/setting-the-horizontal-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_horizontalSpacing_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalGap:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="10"
                        value="2"
                        snapInterval="1"
                        tickInterval="1"
                        liveDragging="true" />
            </mx:FormItem>
            <mx:FormItem label="labelPlacement:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[left,right]"
                        selectedIndex="1" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:LinkButton id="linkButton"
            label="LinkButton"
            icon="@Embed('assets/LinkButton.png')"
            horizontalGap="{slider.value}"
            labelPlacement="{comboBox.selectedItem}" />

</mx:Application>

View source is enabled in the following example.

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/06/setting-the-horizontal-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_horizontalSpacing_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        LinkButton {
            icon: Embed("assets/LinkButton.png");
            horizontalGap: 10;
        }
    </mx:Style>

    <mx:LinkButton id="linkButton"
            label="LinkButton"
            labelPlacement="left" />

</mx:Application>

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

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/06/setting-the-horizontal-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_horizontalSpacing_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;
            import mx.events.SliderEvent;

            private function slider_change(evt:SliderEvent):void {
                linkButton.setStyle("horizontalGap", evt.value);
            }

            private function comboBox_change(evt:ListEvent):void {
                linkButton.labelPlacement = comboBox.selectedItem.toString();
            }
        ]]>
    </mx:Script>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalGap:">
                <mx:HSlider id="slider"
                        minimum="0"
                        maximum="10"
                        value="2"
                        snapInterval="1"
                        tickInterval="1"
                        liveDragging="true"
                        change="slider_change(event);" />
            </mx:FormItem>
            <mx:FormItem label="labelPlacement:">
                <mx:ComboBox id="comboBox"
                        dataProvider="[left,right]"
                        selectedIndex="1"
                        change="comboBox_change(event);" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:LinkButton id="linkButton"
            label="LinkButton"
            icon="@Embed('assets/LinkButton.png')" />

</mx:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/06/setting-the-horizontal-gap-between-an-icon-and-label-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_horizontalSpacing_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.ComboBox;
            import mx.controls.HSlider;
            import mx.controls.LinkButton;
            import mx.events.ListEvent;
            import mx.events.SliderEvent;

            [Embed("assets/LinkButton.png")]
            private const linkButtonIcon:Class;

            private var slider:HSlider;
            private var comboBox:ComboBox;
            private var linkButton:LinkButton;

            private function init():void {
                slider = new HSlider();
                slider.minimum = 0;
                slider.maximum = 10;
                slider.value = 2;
                slider.snapInterval = 1;
                slider.tickInterval = 1;
                slider.liveDragging = true;
                slider.addEventListener(SliderEvent.CHANGE,
                            slider_change);

                comboBox = new ComboBox();
                comboBox.dataProvider = ["left", "right"];
                comboBox.selectedIndex = 1;
                comboBox.addEventListener(ListEvent.CHANGE,
                            comboBox_change);

                var formItem1:FormItem = new FormItem();
                formItem1.label = "horizontalGap:";
                formItem1.addChild(slider);

                var formItem2:FormItem = new FormItem();
                formItem2.label = "labelPlacement:";
                formItem2.addChild(comboBox);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem1);
                form.addChild(formItem2);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChildAt(appControlBar, 0);

                linkButton = new LinkButton();
                linkButton.label = "LinkButton";
                linkButton.setStyle("icon", linkButtonIcon);
                addChild(linkButton);
            }

            private function slider_change(evt:SliderEvent):void {
                linkButton.setStyle("horizontalGap", evt.value);
            }

            private function comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.toString();
                linkButton.labelPlacement = value;
            }
        ]]>
    </mx:Script>

</mx:Application>