View comments | RSS feed

Creating a subclass from the Event class

All events use an event object to transmit information about the event to the event listener, where the base class for all event objects is the flash.events.Event class. When you define a custom event, you can dispatch an event object of type Event, or you can create a subclass of the Event class to dispatch an event object of a different type. You typically create a subclass of the Event class when your event requires you to add information to the event object, such as a new property to hold information that the event listener requires.

For example, the event objects associated with the Flex Tree control include a property named node, which identifies the node of the Tree control associated with the event. To support the node property, the Tree control dispatches event objects of type TreeEvent, a subclass of the Event class.

Within your subclass of the Event class, you can add properties, add methods, set the value of an inherited property, or override methods inherited from the Event class. For example, you might want to set the bubbles property to true to override the default setting of false, which is inherited from the Event class.

You are required to override the Event.clone() method in your subclass. The clone() method returns a cloned copy of the event object by setting the type property and any new properties in the clone. Typically, you define the clone() method to return an event instance created with the new operator.

Suppose that you want to pass information about the state of your component to the event listener as part of the event object. To do so, you create a subclass of the Event class to create an event, EnableChangedEvent, as the following example shows:

package myEvents
{
        import flash.events.Event;

        public class EnableChangedEvent extends Event
        {
    
            // Public constructor. 
            public function EnableChangedEvent(type:String, 
isEnabled:Boolean=false) { // Call the constructor of the superclass. super(type); // Set the new property. this.isEnabled = isEnabled; } // Define a public variable to hold the state of the enable property. public var isEnabled:Boolean; // Override the inherited clone() method. override public function clone():Event { return new EnableChangedEvent(type, isEnabled); } } }

In this example, your custom class defines a public constructor that takes two arguments:

From within the body of your constructor, you call the super() method to initialize the base class properties.


Flex 2

Comments


sparky1962 said on Sep 24, 2006 at 4:28 AM :
Shouldn't clone's return type be EnableChangedEvent not Event?
And didn't all the events in Flex2 with past tense names get changed to the present tense (i.e. EnableChangeEvent)?
smgilson said on Sep 25, 2006 at 2:28 PM :
It has to be Event since this is an override, and the method signatures have to match.

Events do have present tense names - I can update the example.

Stephen
madjiinx said on Apr 23, 2008 at 7:47 PM :
Uh - But do you dispatch such a Custom Event? Syntax please! :-)
smgilson said on Apr 24, 2008 at 2:09 PM :
Dispatching info is two pages later: http://livedocs.adobe.com/flex/2/docs/00001646.html

Stephen Gilson
Flex Doc Team

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/2/docs/00001644.html