TextAreas with default labels. Useful, right?

Here’s the code:

package
{
    import flash.events.FocusEvent;

    import mx.controls.TextArea;
    import mx.events.FlexEvent;

    public class DefaultLabelTextArea extends TextArea
    {
        [Bindable] public var defaultLabel : String;
        [Bindable] public var defaultLabelStyleName : Object;
        private var originalStyleName : Object;

        public function DefaultLabelTextArea ()
        {
            super ();

            addEventListener (FlexEvent.CREATION_COMPLETE,
                onCreationComplete);
            addEventListener (FocusEvent.FOCUS_IN, onFocusIn);
        }

        private function onCreationComplete (event : FlexEvent) : void
        {
            // saves the original style first
            originalStyleName = styleName;

            text = defaultLabel;
            styleName = defaultLabelStyleName;
        }

        private function onFocusIn (event : FocusEvent) : void
        {
            text = "";
            styleName = originalStyleName;

            removeEventListener (FocusEvent.FOCUS_IN, onFocusIn);
        }

    }
}


And a sample implementation:

<component:DefaultLabelTextArea id="textArea"
    defaultLabel="Enter comment…"
    styleName="textArea"
    defaultLabelStyleName="textAreaDefaultLabel" />


This example removes the default label after the user focuses on the component, but it would be easy to modify this to display the label until the user enters some text.