Adobe
Products

Top destinations

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • SiteCatalyst
  • Students
  • Elements family

Adobe Creative Cloud

  • What is Adobe Creative Cloud?
  • Design
  • Web
  • Photography
  • Video
  • Students
  • Teams
  • Enterprise
  • Educational institutions

Design and photography

  • Photoshop
  • Illustrator
  • InDesign
  • Adobe Muse
  • Lightroom

Video

  • Adobe Premiere
  • After Effects

Web development and HTML5

  • Edge Tools & Services [opens in a new window]
  • Dreamweaver
  • Gaming [opens in a new window]

Adobe Marketing Cloud

  • What is Adobe Marketing Cloud?
  • Digital analytics
  • Social marketing
  • Web experience management
  • Testing and targeting
  • Media optimization

Analytics

  • SiteCatalyst
  • Adobe Discover
  • Insight

Social

  • Adobe Social

Experience Manager

  • CQ
  • Scene7

Target

  • Test&Target
  • Recommendations
  • Search&Promote

Media Optimizer

  • AdLens
  • AudienceManager
  • AudienceResearch

Document services

  • Acrobat
  • EchoSign [opens in a new window]
  • FormsCentral [opens in a new window]
  • SendNow [opens in a new window]
  • Acrobat.com [opens in a new window]

Publishing

  • Digital Publishing Suite

  • See all products
Business solutions

By business need

  • Digital analytics
  • Digital publishing
  • Document management
  • Media optimization
  • Social marketing
  • Testing and targeting
  • Video editing and serving
  • Web development [opens in a new window]
  • Web experience management
  • See all business needs

By industry

  • Broadcast
  • Education
  • Financial services
  • Government
  • Publishing
  • Retail
  • See all industries
Support & Learning

I need help

  • Products
  • Adobe Creative Cloud
  • Adobe Marketing Cloud
  • Forums [opens in a new window]

I want to learn

  • Training and tutorials
  • Certification [opens in a new window]
  • Adobe Developer Connection
  • Adobe Design Center
  • Adobe TV [opens in a new window]
  • Adobe Marketing Center
  • Adobe Labs [opens in a new window]
Download
  • Product trials
  • Adobe Flash Player
  • Adobe Reader
  • Adobe AIR
  • See all downloads
Company
  • Careers at Adobe
  • Investor Relations
  • Newsroom
  • Privacy
  • Corporate Social Responsibility
  • Customer Showcase
  • Contact us
  • More company info
Buy
  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers
  • Adobe Marketing Cloud sales [opens in a new window]
Search
 
Info Sign in
Why sign in? Sign in to manage your account and access trial downloads, product extensions, community areas, and more.
Welcome,
My Adobe
My orders
My information
My preferences
My products and services
Sign out
My cart
Privacy My Adobe
Adobe
Products Sections Buy   Search  
Solutions Company
Help Learning
Sign in Sign out Privacy 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
Promotions
Estimated shipping
Tax
Calculated at checkout
Total
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 BuilderFlexRIA
Was this helpful?
Yes   No

By clicking Submit, you accept the Adobe Terms of Use.

 
Thanks for your feedback.

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: Modify the database
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Change the appearance of your application
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Deploy your application to a web server
  • Flex Test Drive: Add charts and graphs
  • Test Drive: Deploy your application to a web server
  • Flex Test Drive: Add charts and graphs
  • Flex Test Drive: Build an application in an hour
  • Flex Test Drive: Modify the database

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
08/04/2011 Disabling auto-complete on Spark ComboBox
01/24/2013 Flash Builder 4.7 and design view question
05/16/2013 Generating a SWF from a .AS file?
05/15/2013 XSS Cross Site Scripting Issue in Flex messagebroker amfsecure URLs

Flex Cookbook

More

Products

  • Adobe Creative Cloud
  • Creative Suite
  • Adobe Marketing Cloud
  • Acrobat
  • Photoshop
  • Digital Publishing Suite
  • Elements family
  • SiteCatalyst
  • For education

Download

  • Product trials
  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR

Support & Learning

  • Product help
  • Forums

Buy

  • For personal and professional use
  • For students, educators, and staff
  • For small and medium businesses
  • Volume Licensing
  • Special offers

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 © 2013 Adobe Systems Incorporated. All rights reserved.

Terms of Use | Privacy | Cookies

Ad Choices

Reviewed by TRUSTe: site privacy statement