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.

 
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.

3 Responses to Customizing a FormItem container’s label in Flex 3

  1. Eric says:

    Thanks lots of.

  2. sundaram says:

    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?

  3. Nathan5.x says:

    Hi Peter.,
    Thanks for the stuff.

    Is it possible to customize the colors in FormItem Label???
    Like giving different color for a single character in the Form Item Label.

    Please share your ideas.

    Advance Thanks.

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