![MACROMEDIA FLEX - DEVELOPING COMPONENTS AND THEMES Manual Download Page 33](http://html1.mh-extra.com/html/macromedia/flex-developing-components-and-themes/flex-developing-components-and-themes_manual_3388115033.webp)
Adding events to custom components
33
If your custom SWC file has properties typed Array, do not use data binding to supply initial
values for array properties. For example, if your custom SWC file has a
labels
property that is
typed Array, do not use data binding in the MXML as the following example shows:
<yourSWC labels={myArray}/>
<mx:Script>
<![CDATA[
var myArray=["cat", "dog", "bird"];
]]>
</mx:Script>
The problem is that Flex instantiates the SWC file before data binding occurs. Instead, you define
the array using the
<mx:Array>
tag, as the following example shows:
<yourSWC>
<labels>
<mx:Array>
<mx:String>cat</mx:String>
<mx:String>dog</mx:String>
<mx:String>bird</mx:String>
</mx:Array>
</labels>
</yourSWC>
Adding events to custom components
All visual controls inherit a large set of events from the base classes, UIObject and UIComponent.
From the UIComponent class, components inherit events such as
focusIn
,
focusOut
,
keyDown
,
and
keyUp
. From the UIObject class, components inherit events such as
mouseDown
,
mouseUp
,
mouseOver
, and
mouseOut
. For a complete list of UIObject and UIComponent events, see
Developing Flex Applications
.
Components can emit and consume events. In most cases, you want your component to emit an
event and the MXML application to consume and handle it.
Custom components that extend existing Flex classes inherit those events. For example, if you
extend the mx.controls.Button class, you have the set of click-related events at your disposal, in
addition to the events that all controls inherit, such as
mouseOver
and
mouseDown
.
This section describes how to add event handling and emitting functionality to your custom
components.
Handling the initialize event
When Flex finishes creating a component, it emits the component’s
initialize
event. This
event is used by MXML developers to populate data, debug, or perform some other function
before the user starts interacting with the application.