18
Aug
08

Setting the button label placement on a RadioButton control in Flex

The following example shows how you can set the button label placement on a Flex RadioButton control by setting the labelPlacement property in MXML or ActionScript.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/18/setting-the-button-label-placement-on-a-radiobutton-control-in-flex/ -->
<mx:Application name="RadioButton_labelPlacement_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

            private const arr:Array = [ButtonLabelPlacement.LEFT,
                                        ButtonLabelPlacement.RIGHT,
                                        ButtonLabelPlacement.TOP,
                                        ButtonLabelPlacement.BOTTOM];
        ]]>
    </mx:Script>

    <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:RadioButton id="radioButton"
            label="RadioButton"
            labelPlacement="{comboBox.selectedItem}" />

</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/08/18/setting-the-button-label-placement-on-a-radiobutton-control-in-flex/ -->
<mx:Application name="RadioButton_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 const arr:Array = [ButtonLabelPlacement.LEFT,
                                        ButtonLabelPlacement.RIGHT,
                                        ButtonLabelPlacement.TOP,
                                        ButtonLabelPlacement.BOTTOM];

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

    <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:RadioButton id="radioButton"
            label="RadioButton" />

</mx:Application>

3 Responses to “Setting the button label placement on a RadioButton control in Flex”


  1. 1 Lucas Zerma Aug 19th, 2008 at 5:50 am

    Simple, but very useful in some cases ..
    congratulations blog.flexexamples.com.

    Att.. Lucas Zerma

  2. 2 Joe Aug 19th, 2008 at 7:48 am

    These examples are a little weak, the label placement is the same for all controls. This post implies it is somehow different.

    You should have more complex examples. I think most people know how to set properties on controls.

  3. 3 Justin Winter Oct 21st, 2008 at 2:01 pm

    How would you position the label 2 or 3 pixels from top of the control? What is the best way to vertically center the text to the radio button? By default the text seems to be top aligned with the button. This is undesirable in many cases.

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".




Badge Farm

  • Powered by Redoable 1.2
  • Cornify
  • Feeds burnt by Feedburner
  • Feed