|
Applications built with Flex are event-driven. User interface
components respond to various events, such as a user clicking a
button or the initialization of an object is complete. You write
event handlers in ActionScript code that define how the component
responds to the event.
Note: You can also generate event handlers for non-visible items
such as RemoteObject and HTTPService.
Flash Builder provides event handler assistance that generates
the event handler functions for a component. Within the generated
function, you write the code that defines the component behavior
in response to the event.
Content assist for an item in the MXML editor lets you access
event handler assistance.
About generated event handlersWhen Flash Builder generates an event handler function,
it places the event handler in the first Script block of the file.
The function is placed at the end of the Script block. The generated
event handler has protected access and accepts the appropriate subclass
of Event as its only parameter.
Flash Builder generates a unique name for the event handler based
on the component’s class name or a custom name for the event handler
that you specify. If you do not specify a custom name, the name
is generated according to the following process:
If an id property is defined, Flash Builder bases the
name on the id property.
If there is no id property defined for the component, Flash
Builder generates a unique name, based on the component’s class
name.
You provide the body of the event handler. The following code
block shows a generated event handler for a Button.
. . .
<fx:Script>
<![CDATA[
protected function myButton_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
<s:Button label="Button" id="myButton" click="myButton_clickHandler(event)"/>
. . .
Flash Builder designates a default event for each user interface
component. For example, the default event for a Button is the click
event.
Generating event handlers for componentsIn an MXML block in the editor, create a component,
but do not specify any events.
Enable content assist for the properties of a component by
typing a space after the class name.
From the list of selected properties, select an event (for
example, doubleClick).
Press Control+Space and select Generate Event Handler.
Flash
Builder generates a unique name for the event handler and places
the event handler in the Script block.
Note: If you specify
a custom name for the event handler, then Flash Builder cannot generate
the handler. If you want to use a custom name, first generate an event
handler and then modify the name of the handler in both the event
property and the generated handler.
Add the code for your
implementation of the event handler to the generated event handler
function.
|
|
|