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 Test Drive /

Flex Test Drive: Build an application in an hour

by Adobe

Adobe logo

Modified

2 May 2011

Page tools

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

Video | Code | Tutorial | Links

Note: Flex Test Drive requires Flex 4.5 SDK and Flash Builder 4.5; download a 60-day trial version of Flash Builder 4.5 before continuing if you haven't already.

Code your interactions

 

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.
  • Download Flash Builder 4.5 Trial
  • Download the Test Drive solution files (ZIP, 14 MB)

Code

<?xml version="1.0" encoding="utf-8"?> <s:Application …> <fx:Script> <![CDATA[ (…) import spark.events.GridSelectionEvent; protected function deptBtn_clickHandler(event:MouseEvent):void { currentState="Departments"; } protected function empBtn_clickHandler(event:MouseEvent):void { currentState="Employees"; } protected function empDg_selectionChangeHandler(event:GridSelectionEvent):void { currentState="EmployeeDetails"; } protected function searchBtn_clickHandler(event:MouseEvent):void { if(searchTxt.text!="") { getEmployeesResult.token = employeeService.getEmployeesByName(searchTxt.text); } else { getEmployeesResult.token = employeeService.getEmployees(); } currentState="Employees"; } protected function toggleBtn_clickHandler(event:MouseEvent):void { if(toggleBtn.selected){ deptDg.setStyle("fontSize",14); toggleBtn.label="Smaller Text"; } else{ deptDg.setStyle("fontSize",12); toggleBtn.label="Bigger Text"; } } ]]> </fx:Script> (…) <s:Button id="empBtn" click.Departments="empBtn_clickHandler(event)" …/> <s:Button id="deptBtn" click="deptBtn_clickHandler(event)" …/> <s:DataGrid id="empDg" selectionChange="empDg_selectionChangeHandler(event)" …> (…) <s:Button id="searchBtn" click="searchBtn_clickHandler(event)" …/> <s:TextInput id="searchTxt" enter="searchBtn_clickHandler(event)" .../> <s:Button id="toggleBtn" includeIn="Departments" x="600" y="100" label="Bigger Text" width="100" click="toggleBtn_clickHandler(event)"/> </s:Application>

Tutorial

In Flex, when a user interacts with a component in a Flex application, the component broadcasts events; for example, a click, rollOver,or rollOut event. To respond to an event, you specify an event handler function to be called when that event occurs.

In this tutorial, you write event handlers to switch between the application states when buttons are clicked, populate the details form when a DataGrid row is selected, and search the employees when a button is clicked or the Enter key pressed, and make the DataGrid text bigger when a button is clicked.

Step 1: Generate an event handler.

Right-click the Departments button and select Generate Click Handler. Make this the click handler for all states.

Flash Builder automatically switches to Source mode and you will see a new function:

protected function deptBtn_clickHandler(event:MouseEvent):void { // TODO Auto-generated method stub }

... and a click event attribute in the Departments Button tag:

<s:Button id="deptBtn" x="140" y="100" label="Departments" enabled.Departments="false" click.EmployeeDetails="deptBtn_clickHandler(event)"/>

When the user clicks this Button in the EmployeeDetails state, that event handler function will be called. Change it, so this is the click handler for all states:

<s:Button id="deptBtn" x="140" y="100" label="Departments" enabled.Departments="false" click ="deptBtn_clickHandler(event)"/>

Step 2: Change to the Departments state on a Button click event.

Inside the function, switch to the Departments state by setting the currentState property of the Application to Departments. Run the application.

The Departments Button click event handler should appear like this:

protected function deptBtn_clickHandler(event:MouseEvent):void { currentState="Departments"; }

When you run the application, you should now be able to click the Departments button to switch to the Departments state (see Figure 1). Note that the Employees button is now enabled and the Departments button is disabled.

In a browser, switch to the Departments state.
Figure 1. In a browser, switch to the Departments state.

Step 3: Change to the Employees state on a Button click event.

With the Departments state selected in Design mode, generate a click event handler for the Employees button and inside the function, switch to the Employees state.

The Employees Button click event handler should appear like this:

protected function empBtn_clickHandler(event:MouseEvent):void { currentState = "Employees"; }

In the Employees Button tag, you can leave the attribute as click.Departments or change it to click.Either works because this Button is only enabled and clickable in the Departments state.

When you run the application, you should now be able toswitch back and forth between the Departments and Employees states. Select arow in the employee DataGrid, nothing happens.

Step 4: Change to the EmployeeDetails state on a DataGrid selectionChange event.

In Source mode, add a selectionChange attribute to empDg DataGrid and generate a selectionChange handler (see Figure 2). Inside the handler, set currentState to EmployeeDetails.

Generate a DataGrid change event handler.
Figure 2. Generate a DataGrid change event handler.

The DataGrid selectionChange event handler should appear as shown here:

protected function empDg_selectionChangeHandler(event:GridSelectionEvent):void { currentState="EmployeeDetails"; }

When you run the application, you should now be able to select a row in the employee DataGrid and see the details for that employee (see Figure 3).

In a browser, select an employee to view the EmployeeDetails state.
Figure 3. In a browser, select an employee to view the EmployeeDetails state.

Step 5: Load new data on a click event.

Using the Data/Services view, configure the getEmployeesByName() operation to return an array of existing Employee objects, Employee[]. Drag the getEmployeesByName()operation onto the Search button. Inside the generated event handler, pass the user-entered value which is held in searchTxt.text to the operation. Change the responder to be the existing getEmployeesResult responder, and change the application to the Employees state.

By default, a new responder getEmployeesByNameResult is created:

<s:CallResponder id="getEmployeesByNameResult"/>

... and used for the service call:

protected function searchBtn_clickHandler(event:MouseEvent):void { getEmployeesByNameResult.token = employeeService.getEmployeesByName(searchTxt.text); }

You want the results to be displayed in the existing empDg DataGrid, though, so change the responder to be the existing getEmployeesResult responder whose lastResult property is already bound to the dataProvider of the DataGrid through the AsyncListView object. You also want to switch the application back to the Employees state until the user selects another employee in the DataGrid.

protected function searchBtn_clickHandler(event:MouseEvent):void { getEmployeesResult.token = employeeService.getEmployeesByName(searchTxt.text); currentState="Employees"; }

You are not using the generated getEmployeesByNameResult responder so you can delete its definition in the Declarations block which is shown here:

<s:CallResponder id="getEmployeesByNameResult"/>

Run the application and enter a last name like Smith in the search field and click Search. When the Search button is clicked, a new service call is made to the server and the responder is populated with this new data so the data displayed in the DataGrid changes. Select an employee and make sure the employee details are displayed (see Figure 4).

Note: If you do not see details for a selected employee, make sure you used the Data/Services view to configure the return type of the getEmployeesByName function to Employee[].

View details.
Figure 4. View details.

In this example, you are calling a method on the server to perform the employee filtering. However, because you already returned all the employee records to the client, you could have just filtered the records locally instead. This would be more efficient because unnecessary calls to the server would be avoided.

Step 6: Use conditional logic to retrieve all or only some records.

Modify the searchBtn click handler so that it calls getEmployeesByName() if searchTxt.text has a value, and getEmployees()if it does not.

Your event handler should appear as shown here:

protected function searchBtn_clickHandler(event:MouseEvent):void { if(searchTxt.text!=""){ getEmployeesResult.token = employeeService.getEmployeesByName(searchTxt.text); } else{ getEmployeesResult.token = employeeService.getEmployees(); } currentState="Employees"; }

Run the application. Search by a name and view the results. Next, remove any search string and search. You should see all the employees listed again.

Step 6: Search employees on an enter event.

Register to listen for the TextInput's enter event and set the event handler to the existing searchBtn_clickHandler() function. Change the event handlers event argument from type MouseEvent to Event.

Your TextInput should appear as shown here:

<s:TextInput id="searchTxt" includeIn="EmployeeDetails,Employees" x="494" y="100" prompt="Last Name" enter="searchBtn_clickHandler(event)"/>

The enter event broadcasts a Flex event. The click event broadcasts a MouseEvent. In order to have the same event handler handle both events, you need to change its argument to the superclass type, Event.

protected function searchBtn_clickHandler(event:Event):void { (...) }

Step 8: Modify object styles on a click event.

Add a ToggleButton control, toggleBtn with the label (in code font) Bigger Text, to the Departments state (see Figure 5). Generate a click handler for it that uses the setStyle() method to increase the DataGrid fontSize style.

Add a Bigger Text ToggleButton to the Departments view.
Figure 5. Add a Bigger Text ToggleButton to the Departments view.

The click handler should appear as shown here:

protected function toggleBtn_clickHandler(event:MouseEvent):void { deptDg.setStyle("fontSize",14); }

You cannot change styles at runtime using the object.property=value notation you use to set properties.Instead, you have to use the setStyle() method. Properties are set and are applicable to only one instance of an object whereas styles can be set in multiple places (inline, in CSS, or inherited), so setting a style on one object may also change the style of other objects. A StyleManager class has to be used to manage all the styles.

Run the application, switch to Departments, and click the Bigger Text button. The text in the DataGrid should get bigger.

Step 9: Decrease the text size when the ToggleButton is deselected.

Inside toggleBtn_clickHandler(), add conditional logic to see if the selected property of toggleBtn is true. If it is, increase the font size and set its label to Smaller Text. If it is not, decrease the text size and set the label to Bigger Text.

The event handler should appear as shown here:

protected function toggleBtn_clickHandler(event:MouseEvent):void { if(toggleBtn.selected){ deptDg.setStyle("fontSize",14); toggleBtn.label="Smaller Text"; } else{ deptDg.setStyle("fontSize",12); toggleBtn.label="Bigger Text"; } }

Set the width of the ToggleButton to 100, so it does not change size as the label changes.

<s:ToggleButton id="toggleBtn" includeIn="Departments" x="600" y="100" width="100" label="Bigger Text" click="toggleBtn_clickHandler(event)"/>

Run the application, switch to the Departments state, and make sure you can successfully increase and decrease the text size.

In this module, you built a Flex application with multiple states that can load and display data and perform database queries. To build on your knowledge, view some of the other Test Drive tutorials that cover topics such as updating your database based on user input, and debugging and deploying your application.

Learn more

Refer to the following resources to learn more about this topic:

  • Learning ActionScript 3

Documentation: Using Flash Builder 4.5

  • Generating event handlers

Documentation: Using Flex 4.5

  • Events

ActionScript 3 Reference

  • ActionScript Statements, Keywords, and Directives

Documentation: Using LiveCycle Data Services 3.1

  • Using Adobe LiveCycle Data Services
  • Using destinations
  • System architecture

Documentation: Using LiveAdobe ColdFusion 9

  • Flex and AIR integration in ColdFusion

Flex Developer Center

  • Handling events

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

  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Modify the database
  • Flex Test Drive: Add charts and graphs
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Add charts and graphs
  • Flex Test Drive: Modify the database
  • Flex Test Drive: Test and debug your code

Tutorials and samples

Tutorials

  • Flex mobile performance checklist
  • Flex and Maven with Flexmojos – Part 3: Journeyman
  • Migrating Flex 3 applications to Flex 4.5 – Part 4

Samples

  • Twitter Trends
  • Flex 4.5 reference applications
  • Mobile Trader Flex app on Android Market

Flex user forum

More
07/25/2011 Flash Player Debug Issues - Safari 5.1 & Chrome 13
04/22/2012 Loader png - wrong color values in BitmapData
04/22/2012 HTTPService and crossdomain.xml doesn't work as expected
04/23/2012 Memory related crashes in Flex application

Flex Cookbook

More
04/06/2012 How to detect screen resize with a SkinnableComponent
02/29/2012 Embed Stage3D content inside Flex application components
02/15/2012 Custom WorkFlow Component
02/09/2012 Using Camera with a MediaContainer instead of VideoDisplay

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