Flex applications are event driven. Events let a programmer know when the user has interacted with an interface component, and also when important changes have happened in the appearance or life cycle of a component, such as the creation or destruction of a component or its resizing.
When an instance of a component dispatches an event, objects that have registered as listeners for that event are notified. You define event listeners, also called event handlers, in ActionScript to process events. You register event listeners for events either in the MXML declaration for the component or in ActionScript. For additional examples of the event handling, see Initializing components at run time.
The following example registers an event listener in MXML that is processed when you change views in an Accordion container.
<?xml version="1.0"?>
<!-- components\CompIntroEvent.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="300"
height="280">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
private function handleAccChange():void {
Alert.show("You just changed views.");
}
]]>
</mx:Script>
<!-- The Accordion control dispatches a change event when the
selected child container changes. -->
<mx:Accordion id="myAcc"
height="60"
width="200"
change="handleAccChange();">
<mx:HBox label="Box 1">
<mx:Label text="This is one view."/>
</mx:HBox>
<mx:HBox label="Box 2">
<mx:Label text="This is another view."/>
</mx:HBox>
</mx:Accordion>
</mx:Application>
The executing SWF file for the previous example is shown below:
This example produces the following image:
You can pass an event object, which contains information about the event, from the component to the event listener.
For the Accordion container, the event object passed to the event listener for the change event is of class IndexChangedEvent. You can write your event listener to access the event object, as the following example shows:
<?xml version="1.0"?>
<!-- components\CompIntroEventAcc.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="300"
height="280">
<mx:Script>
<![CDATA[
// Import the class that defines the event object.
import mx.events.IndexChangedEvent;
import mx.controls.Alert;
private function handleChange(event:IndexChangedEvent):void {
var currentIndex:int=event.newIndex;
Alert.show("You just changed views.\nThe new index is "
+ event.newIndex + ".");
}
]]>
</mx:Script>
<!-- The Accordion control dispatches a change event when the
selected child container changes. -->
<mx:Accordion id="myAcc"
height="60"
width="200"
change="handleChange(event);">
<mx:HBox label="Box 1">
<mx:Label text="This is one view."/>
</mx:HBox>
<mx:HBox label="Box 2">
<mx:Label text="This is another view."/>
</mx:HBox>
</mx:Accordion>
</mx:Application>
The executing SWF file for the previous example is shown below:
In this example, you access the newIndex property of the IndexChangedEvent object to determine the index of the new child of the Accordion container. For more information on events, see Using Events.
About the component instantiation life cycle
The component instantiation life cycle describes the sequence of steps that occur when you create a component object from a component class. As part of that life cycle, Flex automatically calls component methods, dispatches events, and makes the component visible.
The following example creates a Button control and adds it to a container:
<?xml version="1.0"?>
<!-- components\AddButtonToContainer.mxml -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Box id="box1" width="200">
<mx:Button id="button1" label="Submit"/>
</mx:Box>
</mx:Application>
The executing SWF file for the previous example is shown below:
The following ActionScript is equivalent to the portion of the MXML code. Flex executes the same sequence of steps in both examples.
// Create a Box container. var box1:Box = new Box(); // Configure the Box container. box1.width=200; // Create a Button control. var button1:Button = new Button() // Configure the Button control. button1.label = "Submit"; // Add the Button control to the Box container. box1.addChild(button1);
The following steps show what occurs when you execute the ActionScript code to create the Button control, and add it to the Box container. When you create the component in MXML, Flex SDK generates equivalent code.
// Create a Button control. var button1:Button = new Button()
// Configure the button control. button1.label = "Submit";
// Add the Button control to the Box container. box1.addChild(button1);
Flex performs the following actions to process this line:
You can later remove a component from a container by using the removeChild() method. The removed child's parent property is set to null. If you add the removed child to another container, it retains its last known state. If there are no references to the component, it is eventually deleted from memory by the garbage collection mechanism of Adobe® Flash® Player.
Given this sequence of actions, you should use the events as follows: