Adobe Flex 3 Help

About events

Adobe® Flex® applications are event-driven. Events let an application know when the user interacts with the interface, and also when important changes happen in the appearance or life cycle of a component, such as the creation of a component or its resizing. Events can be generated by user input devices, such as the mouse and keyboard, or by the asynchronous operations, such as the return of a web service call or the firing of a timer.

The core class of the Flex component architecture, mx.core.UIComponent, defines core events, such as updateComplete, resize, move, creationComplete, and others that are fundamental to all components. Subclasses of UIComponent inherit these events.

Custom components that extend existing Flex classes inherit all the events of the base class. Therefore, if you extend the Button class to create the MyButton class, you can use the click event and the events that all controls inherit, such as mouseOver or initialize, as the following example shows.

<?xml version="1.0"?>
<!-- events/EventsMyApplication.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    xmlns:MyComp="myComponents.*"> 

    <mx:Script>
        <![CDATA[
            import flash.events.Event;

            // Event listener for the click event. 
            private function handleClick(eventObj:Event):void {
                // Define event listener.
            }

            // Event listener for the initialize event. 
            private function handleInit(eventObj:Event):void {
                // Define event listener.
            }
        ]]>
    </mx:Script>

    <MyComp:MyButton 
        click="handleClick(event);" 
        initialize="handleInit(event);"/>

</mx:Application>

The executing SWF file for the previous example is shown below:

In addition to using the events inherited from its superclasses, your custom components can define custom events. You use custom events to support data binding, to respond to user interactions, or to trigger actions by your component.

For more information on the Flex event mechanism, see Using Events.

Using an event object

When a Flex component dispatches an event, it creates an event object, where the properties of the event object contain information describing the event. An event listener takes this event object as an argument and accesses the properties of the object to determine information about the event.

The base class for all event objects is the flash.events.Event class. All event objects are instances of the Event class, or instances of a subclass of the Event class.

The following table describes the public properties of the Event class. The Event class implements these properties using getter methods.

Property

Type

Description

type

String

The name of the event; for example, "click". The event constructor sets this property.

target

EventDispatcher

A reference to the component instance that dispatches the event. This property is set by the dispatchEvent() method; you cannot change this to a different object.

currentTarget

EventDispatcher

A reference to the component instance that is actively processing the Event object. The value of this property is different from the value of the target property during the event capture and bubbling phase. For more information, see Using Events.

eventPhase

uint

The current phase in the event flow. The property might contain the following values:

  • EventPhase.CAPTURING_PHASE: The capture phase
  • EventPhase.AT_TARGET: The target phase
  • EventPhase.BUBBLING_PHASE: The bubbling phase
bubbles

Boolean

Whether an event is a bubbling event. If the event can bubble, the value for this property is true; otherwise, it is false. You can optionally pass this property as a constructor argument to the Event class. By default, most event classes set this property to false. For more information, see Using Events.

cancelable

Boolean

Whether the event can be canceled. If the event can be canceled, the value for this value is true; otherwise, it is false. You can optionally pass this property as a constructor argument to the Event class. By default, most event classes set this property to false. For more information, see Using Events.