Generating event handlers
Flex applications 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.
You access event handler assistance in three ways:
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. You can specify the event handler for the default event in
the Standard View of the Properties View. To specify handlers for
other events, in the Property Inspector select Category View >
Events.
You can also use content assist in the Source View to generate
event handlers.
Generating event handlers using the Properties ViewIn Design mode, select an item and then select Standard View
in the Properties Inspector.
An editing field for the default
event handler is visible in the Common area.
To generate an event handler for the default event:
(Optional) In the On Event text field, specify a name
for the event.
For example, in the On Click text field for
a Button component, specify MyButtonClick.
If
you do not specify a name, Flash Builder generates a unique name
for the event.
 When specifying a name for
the event handler, you have the option to specify an event parameter.
If you do not specify the event parameter, Flash Builder generates
the parameter with an appropriate event type.
Click the “lightening bolt” icon, and select Generate Event
Handler.
The editor switches to Source mode, with the body
of the generated event handler highlighted. Type in your implementation
for the event.
To generate an event handler for any event for a selected
item:
Select Category View and expand the Events node
to view all the events for the item.
(Optional) Double-click the name of the event to activate
the text box for the event handler name. Type the name for the event
handler.
Click the icon in the Value field to create the event handler.
The
editor switches to Source mode, with the body of the generated event handler
highlighted. Type in your implementation for the event.
Generating event handlers using the context menu for an itemIn Design View, open the context menu
for an item.
Perform one of the following actions:
Select
the default event for the item.
For example, for a Button
select Generate Click Handler.
Select Show All Events to open the list of events in the
Properties view.
Specify an event handler from the Properties
view.
The editor switches to Source mode
with the body of the generated event handler highlighted. Type in
your implementation for the event.
Generating event handlers using content assistIn an MXML block in code view, 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.
|
|