The following example shows how you can position the icon and label in a Flex LinkButton control by setting the labelPlacement property to one of the static constant values in the ButtonLabelPlacement class.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/05/positioning-icons-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_labelPlacement_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.controls.ButtonLabelPlacement;
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:String>{ButtonLabelPlacement.LEFT}</mx:String>
        <mx:String>{ButtonLabelPlacement.RIGHT}</mx:String>
        <mx:String>{ButtonLabelPlacement.TOP}</mx:String>
        <mx:String>{ButtonLabelPlacement.BOTTOM}</mx:String>
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="labelPlacement:">
                <mx:ComboBox id="comboBox"
                        dataProvider="{arr}"
                        selectedIndex="1" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

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

</mx:Application>

View source is enabled in the following example.

You can also set the labelPlacement property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/09/05/positioning-icons-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_labelPlacement_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

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

    <mx:Array id="arr">
        <mx:String>{ButtonLabelPlacement.LEFT}</mx:String>
        <mx:String>{ButtonLabelPlacement.RIGHT}</mx:String>
        <mx:String>{ButtonLabelPlacement.TOP}</mx:String>
        <mx:String>{ButtonLabelPlacement.BOTTOM}</mx:String>
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="labelPlacement:">
                <mx:ComboBox id="comboBox"
                        dataProvider="{arr}"
                        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/05/positioning-icons-on-a-linkbutton-control-in-flex/ -->
<mx:Application name="LinkButton_labelPlacement_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.ButtonLabelPlacement;
            import mx.controls.ComboBox;
            import mx.controls.LinkButton;
            import mx.events.ListEvent;

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

            private var arr:Array;

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

            private function init():void {
                arr = [];
                arr.push(ButtonLabelPlacement.LEFT);
                arr.push(ButtonLabelPlacement.RIGHT);
                arr.push(ButtonLabelPlacement.TOP);
                arr.push(ButtonLabelPlacement.BOTTOM);

                comboBox = new ComboBox();
                comboBox.dataProvider = arr;
                comboBox.selectedIndex = 1;
                comboBox.addEventListener(ListEvent.CHANGE,
                            comboBox_change);

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

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

                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 comboBox_change(evt:ListEvent):void {
                var value:String = comboBox.selectedItem.toString();
                linkButton.labelPlacement = value;
            }
        ]]>
    </mx:Script>

</mx:Application>
 
Tagged with:
 
About The Author

Peter deHaan

Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.

2 Responses to Positioning icons on a LinkButton control in Flex

  1. Atul Singh Parihar says:

    Really its very usefully example

    thanks
    Atul Singh Parihar
    mumbai

  2. Saurabh Jain says:

    Helpful example.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree