Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
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 in a Week /

Exercise 2.1: Handling a user event

by Trilemetry

Trilemetry
  • Trilemetry, Inc.

Content

  • Add a Label and Button to the application
  • Generate an event handler function

Modified

2 May 2011

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
Flash Builder Flex RIA

Requirements

Prerequisite knowledge

  • Exercise 1.1: Setting up Flash Builder and your project files

User level

Beginning

Required products

  • Flash Builder 4.5 Premium (Download trial)

Sample files

  • ex2_01_starter.zip
  • ex2_01_solution.zip

In this exercise you will use inline ActionScript to display the date selected from a DateChooser control. You will then use a function to achieve the same result as the inline ActionScript (see Figure 1).

This is the finished application.
Figure 1. This is the finished application.

In this exercise you will learn how to:

  • Add a Label and Button to the application
  • Generate an event handler function

Add a Label and Button to the application

In this section you will use inline ActionScript to handle a change in the application.

  1. Download the ex2_01_starter.zip file if you haven't already and extract the file ex2_01_starter.fxp to your computer.
  2. Open Flash Builder.
  3. Import the ex2_01_starter.fxp file.
  4. Open the ex2_01_starter.mxml file.
  5. In Design mode, select the DateChooser control for the Pickup Date.
  6. In the Properties view, assign the DateChooser control's id property a value of pickupDate (see Figure 2).
Select the DateChooser control and assign an id of pickupDate.
Figure 2. Select the DateChooser control and assign an id of pickupDate.
  1. Switch to Source mode.
  2. Below the Script comment, create a Script block.
  3. Within the Script block, import the Alert class:
import mx.controls.Alert;
  1. Locate the pickupDate DateChooser control.
  2. To the control, add the change property assigned to run the show() method of the Alert package:
<mx:DateChooser id="pickupDate" change="Alert.show()" ... />
  1. Use the Alert.show() function to display the text You have selected, followed by the date selected from the pickupDate DateChooser control, using the toDateString() function; this displays the date as a string.

    Note: Use single quotes inside of the Alert.show() method, because the change event already uses double quotes.

<mx:DateChooser id="pickupDate" change="Alert.show('You have selected ' + pickupDate.selectedDate.toDateString())" .../>
  1. Save the file.
  2. Run the application.
  3. From the pickupDate DateChooser control, click a date.

    You should see the Alert window with the date you selected (see Figure 3).

Choose a date and the Alert window will open.
Figure 3. Choose a date and the Alert window will open.
  1. Return to Flash Builder.
  2. To the DateChooser control for the Return Date, add the id property with a value of returnDate:
<mx:DateChooser id="returnDate" ... />
  1. Copy the change property and its value from the pickupDate DateChooser control and paste it within the opening tags for the returnDate DateChooser control.
<mx:DateChooser id="returnDate" change="Alert.show('You have selected ' + pickupDate.selectedDate.toDateString())" ... />
  1. Change the instance name reference within the Alert.show() method to returnDate.
<mx:DateChooser id="returnDate" change="Alert.show('You have selected ' + returnDate.selectedDate.toDateString())" ... />
  1. Save the file and run the application.

You should see that the date selected is displayed using the Alert window for both DateChooser controls.

Generate an event handler function

In this section, you will create a function to handle the change event for a DateChooser control.

  1. Return to Flash Builder.
  2. Locate the Script block.
  3. Below the import statement, create a private function named dateChangeHandler() that takes no parameters and returns a void data type.
private function dateChangeHandler():void { }
  1. Locate the pickupDate DateChooser control and cut the Alert.show() method from the change event.
  2. Replace it with a call to the dateChangeHandler() function:
<mx:DateChooser id="pickupDate" change="dateChangeHandler()" ... />
  1. In the Script block and within the dateChangeHandler() function, paste the Alert.show() method that you cut from the change event. Remember to append a semicolon.
private function dateChangeHandler():void { Alert.show('You have selected ' + pickupDate.selectedDate.toDateString()); }
  1. Save the file.
  2. Run the application.
  3. Click a date within the pickupDate DateChooser control.

    The Alert message displays the date you selected (see Figure 4).

Select a start date to open the Alert window.
Figure 4. Select a start date to open the Alert window.
  1. Return to Flash Builder.
  2. Locate the returnDate DateChooser control.
  3. For the change property, replace the value with a call to the dateChangeHandler() function:
<mx:DateChooser id="returnDate" change="dateChangeHandler()" />
  1. Save the file and run the application.
  2. Without selecting a pickup date, select a return date.

You should see the error display shown in Figure 5.

View the error that occurs when a return date is selected.
Figure 5. View the error that occurs when a return date is selected.

You will fix this error in the next exercise.

Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License+Adobe Commercial Rights

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

More Like This

  • Exercise 5.9: Skinning the SkinnableDataContainer container
  • Exercise 5.10: Creating a production build
  • Exercise 5.7: Creating a vertical title bar on a Panel container
  • Exercise 1.1: Setting up your project files
  • Exercise 1.6: Creating MXML custom components with ActionScript properties
  • Exercise 2.2: Using the event object
  • Exercise 2.3: Using the addEventListener() method
  • Exercise 2.4: Populating an ArrayCollection with retrieved data using the result event
  • Exercise 4.5: Creating and using item renderers and item editors
  • Exercise 4.6: Navigating using navigator containers

Products

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • Mobile Apps
  • Photoshop
  • Touch Apps
  • Student and Teacher Editions

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