This chapter discusses many aspects of custom components. It includes examples of using custom events in components and examples of all of the different types of metadata tags that can be used in custom components. It also includes examples of how to compile and deploy your custom components using both the free Flex SDK and Flex Builder.
Custom Events in Components
The ability to define and dispatch events from custom components makes it very easy to get feedback from your components and adjust your application. Adding a custom event to a component is a very simple thing to do. First, define the events within metadata tags at the top of the class definition and then dispatch the event when the criteria you determined necessary have been met.
Dispatching Custom Events
TextInputWithCountEvent.as
package { import mx.controls.TextInput; import flash.events.Event;
[Event(name=”lengthOf5Event”, type=”flash.events.Event”)]
[Event(name=”lengthOf10Event”, type=”flash.events.Event”)]
public class TextInputWithCountEvent extends TextInput
{ private var _myText:String; private var eventObj:Event;
public function set myText(s:String):void{ _myText = s;
this.text = s; if(s.length == 5){ // Create and dispatch custom event eventObj = new Event(”lengthOf5Event”); dispatchEvent(eventObj); }
else if (s.length==10){ eventObj = new Event(”lengthOf10Event”);
dispatchEvent(eventObj);
} } } }
This class declares two custom events by using the Event metadata tag right above the class declaration. There is additional information on the Event metadata tag later in this chapter. The set myText function accepts a String as an argument and sets the text property of the class to the value. It also will dispatch an event when the length of the String is 5 or 10 characters. To dispatch the event, first instantiate the event object and then use the dispatchEvent method to dispatch the event.
The main file, shown in, creates an instance of the TextInputWithCountEvent custom component and adds event handlers to respond when a custom event is dispatched. The event handler functions simply to show an Alert to demonstrate that they received the dispatched event.
TextInputLengthEventSample.mxml
<?xml version=”1.0″ encoding=”utf-8″?> <mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml” xmlns:comps=”*”
backgroundColor=”#FFFFFF”> <mx:Script>
<![CDATA[ import mx.controls.Alert; import flash.events.Event; //
method to handle custom event public
function lengthOf5EventHandler(eventObj:Event):void
{ mx.controls.Alert.show(”Length is 5″); } public function lengthOf10EventHandler(eventObj:Event):void { mx.controls.Alert.show(”Length is 10″); } ]]> </mx:Script>
<mx:Panel title=”Text Input Length Event Sample” width=”300″ height=”150″ paddingTop=”10″ paddingLeft=”10″ paddingRight=”10″ paddingBottom=”10″
layout=”absolute”> <!–Instantiate custom class and define method to handle labelChanged event–> <comps:TextInputWithCountEvent id=”myTI” myText=”{myTI.text}” lengthOf5Event=”lengthOf5EventHandler(event);” lengthOf10Event=
“lengthOf10EventHandler(event);” horizontalCenter=”0″ verticalCenter=”0″/> </mx:Panel> </mx:Application>