21
Mar
08

Customizing a FormItem container’s label in Flex 3

The following example shows how you can access the Label subcomponent in a Flex FormItem container and do fancy stuff like have form labels with hyperlinks or selectable text by using the itemLabel property.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/21/customizing-a-formitem-containers-label-in-flex-3/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.controls.Label;
            import mx.controls.Alert;
            import mx.containers.FormItem;

            private function init2(evt:FlexEvent):void {
                var lbl:Label = FormItem(evt.currentTarget).itemLabel as Label;
                lbl.selectable = true;
            }

            private function init3(evt:FlexEvent):void {
                var lbl:Label = FormItem(evt.currentTarget).itemLabel as Label;
                lbl.htmlText = txt;
                lbl.selectable = true;
                lbl.addEventListener(TextEvent.LINK, label_link);
            }

            private function label_link(evt:TextEvent):void {
                Alert.show("You clicked a link. Have some cake.");
            }
        ]]>
    </mx:Script>

    <mx:String id="txt">
        <![CDATA[<a href="event:att"><u>Hyperlink</u></a> label]]>
    </mx:String>

    <mx:Form maxWidth="500">
        <mx:FormItem id="formItem1"
                label="Default label">
            <mx:Label id="label1"
                    text="The quick brown fox jumped over the..." />
        </mx:FormItem>
        <mx:FormItem id="formItem2"
                label="Selectable label"
                creationComplete="init2(event);">
            <mx:Label id="label2"
                    text="The quick brown fox jumped over the..." />
        </mx:FormItem>
        <mx:FormItem id="formItem3"
                label="Hyperlink label"
                creationComplete="init3(event);">
            <mx:Label id="label3"
                    text="The quick brown fox jumped over the..." />
        </mx:FormItem>
    </mx:Form>

</mx:Application>

View source is enabled in the following example.


2 Responses to “Customizing a FormItem container's label in Flex 3”


  1. 1 Eric Mar 23rd, 2008 at 2:48 pm

    Thanks lots of.

  2. 2 sundaram Apr 7th, 2009 at 12:39 am

    i am Getting an error like
    1119: Access of possibly undefined property itemLabel through a reference with static type

    i am using Flex Gumbo.

    any suggestion?

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