Customizing a FormItem container’s label in Flex 3

by Peter deHaan on March 21, 2008

in FormItem, Forms, Label

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 comments… read them below or add one }

1 Eric March 23, 2008 at 2:48 pm

Thanks lots of.

Reply

2 sundaram April 7, 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?

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can 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

Previous post:

Next post: