Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
More products
Solutions
Digital marketing
Digital media
Education
Financial services
Government
Web Experience Management
More solutions
Learning Help Downloads Company
Buy
Home use for personal and home office
Education for students, educators, and staff
Business for small and medium businesses
Licensing programs for businesses, schools, and government
Special offers
Search
 
Info Sign in
Welcome,
My cart
My orders My Adobe
My Adobe
My orders
My information
My preferences
My products and services
Sign out
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out My orders My Adobe
Preorder Estimated Availability Date. Your credit card will not be charged until the product is shipped. Estimated availability date is subject to change. Preorder Estimated Availability Date. Your credit card will not be charged until the product is ready to download. Estimated availability date is subject to change.
Qty:
Purchase requires verification of academic eligibility
Subtotal
Review and Checkout
Adobe Developer Connection / Flex Developer Center / Flex Quick Starts /

Handling events

by Adobe

Adobe logo

Created

22 March 2010

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flex

Requirements

User level

All

Adobe Flex applications are event-driven. Events let a programmer know when the user interacts with an interface component, and also when important changes occur 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 you 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.

There are three ways to receive event notifications:

  • Registering an event handler in MXML
  • Creating an inline event handler in the MXML definition
  • Registering an event listener through ActionScript

Registering an event handler in MXML

The first, and most widely used method for getting event notifications is to define an event handler in MXML that is called when an event occurs.

In this example, you define an event handler for the click event of a Button control. When a user clicks the Button control, the event handler sets the text property of the Label control to "Hello, World!".

Tip: Flash Builder 4 provides tooling that generates stubs for event handlers. For more information refer to Generating event handlers in the Adobe Flash Builder 4 Help.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- HandlingEventsEventHandler --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="200" height="200"> <fx:Script> <![CDATA[ import flash.events.MouseEvent; private function clickHandler ( event:MouseEvent ):void { myLabel.text = "Hello, World!"; } ]]> </fx:Script> <s:Panel title="My Application" width="100%"> <s:layout> <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/> </s:layout> <s:Label id="myLabel" fontWeight="bold" fontSize="24"/> <s:Button id="myButton" label="Click Me!" click="clickHandler(event);" /> </s:Panel> </s:Application>

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Back to top

Creating an inline event handler in the MXML definition

Sometimes the easiest way of responding to events is to define the event handler entirely in the MXML definition of a component. This is known as using an inline event handler.

In the following example, you set the click property of the <s:Button> tag so that it directly sets the text property of the Label control, without calling an event handler method.

Tip: Using inline event handlers may be quick and result in less code, but they also result in code that is harder to read, maintain, and scale. A good rule of thumb is not to include more than one ActionScript statement in an inline event handler. If you must include more complex logic, place it in an ActionScript helper method or in an ActionScript event handler.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- HandlingEventsInlineMethod.mxml --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="200" height="200"> <s:Panel title="My Application" width="100%"> <s:layout> <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/> </s:layout> <s:Label id="myLabel" fontWeight="bold" fontSize="24"/> <s:Button id="myButton" label="Click Me!" click="myLabel.text = 'Hello, World!'" /> </s:Panel> </s:Application>

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Back to top

Registering an event listener through ActionScript

You can also respond to events by creating an event handler using ActionScript, and then registering an event listener for the event.

In this example, you use ActionScript to create an event handler for the creationComplete event of the Application container. You then register a listener for your event handler by using the addEventListener() method.

Tip: The creationComplete event for the Application form occurs at the start of your application, after the Application container and its children are initialized. The event handler for the creationComplete event provides a convenient place to run ActionScript code to register event listeners.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- HandlingEventsActionScript.mxml --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" width="200" height="200" creationComplete="creationCompleteHandler(event);"> <fx:Script> <![CDATA[ import flash.events.MouseEvent; import mx.events.FlexEvent; private function creationCompleteHandler(event:FlexEvent):void { // Listen for the click event on the Button control myButton.addEventListener (MouseEvent.CLICK, clickHandler); } private function clickHandler ( event:Event ):void { myLabel.text = "Hello, World!"; } ]]> </fx:Script> <s:Panel title="My Application" width="100%"> <s:layout> <s:VerticalLayout verticalAlign="middle" horizontalAlign="center"/> </s:layout> <s:Label id="myLabel" fontWeight="bold" fontSize="24"/> <s:Button id="myButton" label="Click Me!" /> </s:Panel> </s:Application>

Result

This content requires Flash To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player. To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Back to top

For more information

  • Using Events
  • Generating event handlers

Back to top


This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps

Solutions

  • Digital marketing
  • Digital media
  • Web Experience Management

Industries

  • Education
  • Financial services
  • Government

Help

  • Product help centers
  • Orders and returns
  • Downloading and installing
  • My Adobe

Learning

  • Adobe Developer Connection
  • Adobe TV
  • Training and certification
  • Forums
  • Design Center

Ways to buy

  • For personal and home office
  • For students, educators, and staff
  • For small and medium businesses
  • For businesses, schools, and government
  • Special offers

Downloads

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

Company

  • News room
  • Partner programs
  • Corporate social responsibility
  • Career opportunities
  • Investor Relations
  • Events
  • Legal
  • Security
  • Contact Adobe
Choose your region United States (Change)
Choose your region Close

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy Policy and Cookies (Updated)

Ad Choices

Reviewed by TRUSTe: site privacy statement