Adobe
Products
Acrobat
Creative Cloud
Creative Suite
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
Student and Teacher Editions
More products
Solutions
Creative tools for business
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 / Flash Developer Center /

Getting started with Flash CS4 user interface components

by Bob Berry

Bob Berry

Content

  • Adding user interface components to your Flash application
  • Setting component properties and parameters
  • Changing a component's default size and its location on the Stage
  • Handling events
  • Small shopping cart example
  • Where to go from here

Modified

16 February 2009

Page tools

Share on Facebook
Share on Twitter
Share on LinkedIn
Bookmark
Print
ActionScript components events Flash Professional CS4 UI

Requirements

Prerequisite knowledge

General experience with programming in ActionScript 3 is suggested.

User level

Beginning

Required products

  • Flash Professional CS4 (Download trial)

Sample files

  • ButtonExample.zip (745 KB)
  • SmallShoppingCart.zip (1350 KB)

The Adobe Flash CS4 Professional User Interface components are ActionScript 3.0 class objects that implement commonly used pieces of the user interface of a web page. These components help you accelerate development of your web pages by providing elements that you can easily plug in and customize. Flash CS4 provides the following user interface components:

Flash CS4 user interface components
Component Description
Button A resizable, rectangular button that a user can click or press to initiate an action. For example, you can use a Button to send the user to a particular location, such as forward to a new web page or back to a previous one, or to indicate a choice as with an OK or a Submit button.
CheckBox A square box that a user can click to add a check mark to indicate a choice or selection.
ColorPicker A box that opens to allow the user to choose a color from a palette of color swatches.
ComboBox A combination of a text field and a drop-down list from which a user can select an item.
DataGrid A grid of rows and columns for displaying data, which you can load from an array or an XML file that you supply in the form of a DataProvider.
Label A single line of text, typically to identify an element on a web page.
List A scrollable list box from which a user can make one or more selections.
NumericStepper Allows a user to step through an ordered set of numbers. Can be used for selecting dates or specifying a quantity or amount.
ProgressBar Indicates the progress of loading content to the web page.
RadioButton Round buttons that are provided in a group from which a user can select only one. Selecting one RadioButton unselects the previously selected RadioButton in the group.
ScrollPane For displaying content in a scrollable pane when the content would take up too much space on the web page.
Slider Lets a user select a value by sliding a graphical thumb between the end points of a track that corresponds to a range of values.
TextArea Can display or receive multiple lines of text and wrap long lines of text.
TextInput Receives a single line of text input by a user.
TileList A list that is made up of rows and columns that are supplied with data by a DataProvider.
UILoader A container that can display SWF, JPEG, progressive JPEG, PNG, and GIF files.
UIScrollBar Allows you to add a scroll bar to a text field.

This article covers the following topics: adding user interface components to your Flash application, setting component properties and parameters, changing a component's default size and its location on the Stage, and handling events. It concludes with a small shopping cart example.

Adding user interface components to your Flash application

There are two ways to add a component to a Flash application:

  • Using the Flash authoring tool during development
  • Using ActionScript as the application is executing

Using the Flash authoring tool during development

The following procedure shows how to add a Button component to your Flash application during development. You can use the same process to add any of the User Interface components to your application.

To add a Button component to your Flash application during development

  1. If the Components panel is not open, select Window > Components.
  2. Either double-click the Button in the Components panel or drag it with the mouse from the Components panel to the Stage.
  3. If the Component inspector is not open, select Window > Component Inspector.
  4. With the Button selected on the Stage, click the label value in the Component inspector and enter Accept for the label parameter.
  5. Select Control > Test Movie or press Control+Enter (Windows) or Command+Enter (Mac OS) to compile the document.

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.

To get the source files for this example, download ButtonExample.zip at the top of this page.

Using ActionScript as the application is executing

  1. During authoring, place the Button in the library by dragging it from the Components panel (Window > Components) and dropping it in the Library panel (Window > Library).
  2. Add an import statement to the Actions panel on the first frame of the Timeline to make the component's class available to your application. For the Button component, you would add the following import statement:
import fl.controls.Button;
  1. In the Actions panel, create an instance of the component by invoking the component’s ActionScript constructor. For example, the following statement creates an instance of a Button called aButton:
var aButton:Button = new Button();
  1. In the Actions panel, call the static addChild() method to add the component instance to the Stage or application container. For example, the following statement adds the Button instance aButton to the current display container:
addChild(aButton); aButton.label = "Accept";

Setting component properties and parameters

Each component's class has properties that you can read to obtain information about the component or write to affect the component's appearance and behavior. Generally, you set generic properties common to all components, such as position, dimension, color effect, and blending mode in the Property inspector. You set component-specific properties in the Parameters tab of the Component inspector. When you set a parameter in the Flash authoring tool, you are setting the corresponding property in the ActionScript class.

You can directly access and set a component's properties using ActionScript. Setting a property in ActionScript overrides a value that was set using the Property inspector or the Component inspector.

Note: To assign a name to a component instance you must use either the Property inspector or ActionScript

To set parameters using the Property inspector

  1. Using the mouse, either double-click a component in the Components panel or click it and drag it to the Stage.
  2. If the Property inspector panel is not open, select Window > Properties to open it.
Properties inspector
  1. In the Property inspector, enter a name for the component instance in the text input field that contains the prompt <Instance Name>.

To set parameters using the Component inspector

  1. Using the mouse, either double-click the component in the Components panel or click it and drag it to the Stage.
  2. If the Component inspector panel is not open, select Window > Component Inspector to open it.
Component inspector
  1. On the Parameters tab, Flash lists the component's parameters in the Name column and their values in the Value column. You can change the default value to assign a new value to a parameter. The image above shows the Component inspector for the Button component. The parameters vary for different components. For the ComboBox component, where you would see different parameters displayed in the Component inspector, you could change the value of the editable property to true to make the text field editable. Or you could set rowCount to set the maximum number of rows that can appear in the drop-down list.

To set properties using ActionScript

In ActionScript, you use the dot (.) operator to access a component's properties. For example, to implement word wrapping for a Label called titleLabel, you would write the following line of ActionScript code to set the wordWrap property:

titleLabel.wordWrap = true;

To place the Label 100 pixels from the left of the Stage and 50 pixels from the top, you could add the following code to set the x and y properties:

titleLabel.x = 100; titleLabel.y = 50;

You would place this code in the Actions panel (Window > Actions) as shown below:

Actions panel

Changing a component's default size and its location on the Stage

You can change a component's size and location on the Stage in the following ways:

  • Using the Flash authoring tool during development
  • Setting parameters during development
  • Using ActionScript

Using the Flash authoring tool during development

The easiest way to change a component's default size is by using the Free Transform tool, located in the Tools panel (Window > Tools). The Free Transform tool looks like this:

Free transform tool

To change a component's size using the Free Transform tool

  1. Click the Free Transform tool in the Tools palette.
  2. Click the component instance on Stage to select it.
  3. Click and drag the handles that appear at the corners and at the midpoint of each side until the desired size is achieved. The new height (H) and width (W) dimensions appear in the Property inspector.

To change a component's location on the Stage using the Flash authoring tool, simply click the component and drag it to the desired location. When you release the component, you can see the x (horizontal) and y (vertical) coordinates of the new location in the Property inspector. You can make finer adjustments using the keyboard. You can make horizontal adjustments of one pixel by holding down the Ctrl key (Windows) or the Command key (Mac OS) and pressing the left or right arrow keys. You can adjust the vertical location 10 pixels at a time by holding down the Shift key and pressing the up arrow or down arrow keys.

Setting parameters during development

You can also specify a component's size and location by setting parameters in the Property inspector.

To change a component's size and location by setting parameters

Each component's dimensions are shown as width (W) and height (H) values in the Property inspector. You can change the size of a component instance by changing the values of these W and H parameters.

Each component's location on the Stage is shown as x (horizontal) and y (vertical) coordinates in the Property inspector. These parameters correspond to the class's x and y properties. The horizontal position represents the number of pixels from the left edge of the Stage and the vertical position represents the number of pixels from the top. You can change the location of a component instance on the Stage by changing the values of the X and Y parameters.

Using ActionScript

You can also specify a component's size and location using ActionScript.

To change a component's size and location using ActionScript

You can change the size of a component instance by setting its height and width properties and you can change its location by setting its x and y properties. For example, the following lines of code specify the size and location of a List component called citiesList:

citiesList.height = 100; citiesList.width = 125; citiesList.x = 100; citiesList.y = 50;

Handling events

Components communicate with the application that contains them by dispatching events. Events are noteworthy occurrences in your Flash application that you might want to handle with ActionScript code. For example, an event occurs when a user clicks a button or enters characters in a text field. Events also occur when a user scrolls using the UIScrollBar component and when the UILoader component completes a loading operation. Each component class defines the events that occur for that component. The ColorPicker class, for example, has the following events, in addition to the events that it inherits from its parent classes:

ColorPicker events
Event Descripton
change Dispatched when the user clicks a color in the palette.
close Dispatched when the user closes the color palette.
enter Dispatched when the user presses the Enter key after editing the internal text field of the ColorPicker component.
itemRollOut Dispatched when the user rolls out of a swatch in the color palette.
itemRollOver Dispatched when the user rolls over a swatch in the color palette.
open Dispatched when the user opens the color palette.

Handling an event refers to taking an action in response to the event. Handling an event consists of listening for the event and executing the actions you want to take in response.

To listen for an event you must add an event listener for the component instance on which the event occurs. In ActionScript 3.0, use the addEventListener() method to add an event listener. When you add an event listener, you specify the event that you are listening for and the name of a function to call when the event occurs. The function is the event handler, which you must provide. For example, the following line of code adds an event listener that listens for a change event on a List instance called laundryList:

laundryList.addEventListener(Event.CHANGE, howMuch);

The first parameter, Event.CHANGE, is a constant in the Event class that specifies the name of the change event. The second parameter, howMuch, is the name of the function to call when the event occurs.

When a change event does occur on the laundryList instance, Flash calls the function howMuch() and passes it an event object that contains information about the event. The base event object is defined by the Event class.

To implement the event handling function howMuch(), define the function and add the logic that you want to execute. The following implementation of the howMuch() function accesses the data field of the selected list item and adds it to a variable called total. The function casts selectedItem.data to a Number type because in the Event object the data property is defined as a String and the operation would generate an error otherwise:

var total:Number = 0; function howMuch(event_obj:Event):void { total += Number(event.target.selectedItem.data); }

Small shopping cart example

The following example creates an abbreviated shopping cart application using Flash CS4 User Interface components. It uses Label instances for all of the text prompts. It uses a variety of other components to build the user interface, including:

  • Two TextArea instances that describe the item being purchased and provide a checkout summary when you click the Checkout button
  • Two RadioButtons to allow you to choose a men's or women's parka
  • A NumericStepper to let you select the quantity
  • Two ComboBox instances to let you make size and shipping choices
  • A ColorPicker instance to provide an array of available colors for the item

The item description is provided through the text parameter of the item TextArea (itemTa). The data in the Size (sizeCb) and Shipping (shipCb) ComboBoxes is created during authoring by double clicking the value cell for the dataProvider parameters in the Property inspector and adding items to the Values collection dialog. The values seen in the Size ComboBox were added simply by filling in the label parameter with the values that the ComboBox displays. The following illustration shows the values provided to the dataProvider parameter for the Shipping ComboBox.

Shipping CB values

The example also requires the following ActionScript code, which:

  • Sets the colors for the ColorPicker and disables its text field
  • Adds an event listener for a MouseEvent.CLICK event on the Checkout button
  • Implements the checkOut() function to perform the following tasks when the event occurs:

    • Creates the total variable to calculate the total amout due for the order
    • Checks to see that the user has selected a size
    • Assembles the content of the order summary in the text property of the checkout summary TextArea (summaryTxt)
    • Calculates the total amount of the order by multiplying the quantity by the item amount and adding the shipping charge
    • Displays the total amount due by appending the value of the total variable to summaryTxt.text
    • Makes the order summary TextArea (summaryTxt) visible

Here is the source code for the example application. Note that this source code depends on having a number of components already on the stage. Please download the source file to see how the components are placed.

// Sets colors for the ColorPicker itemCp.colors = [0x000000, 0x990000, 0x333366, 0x336633]; // Hides the text input field for the color picker itemCp.showTextField = false; // Hides the checkout summary text summaryTxt.visible = false; // Listens for a click on the checkout button checkoutBtn.addEventListener(MouseEvent.CLICK, checkOut); /** * Assembles the checkout summary text. */ function checkOut(event_obj:MouseEvent):void { // var total:Number = 0; // First makes sure a size is selected if(sizeCb.selectedItem == null) { summaryTxt.text = "Please select a size!"; summaryTxt.visible = true; return; } summaryTxt.text = String(qtyNs.value) + " "; // Identifies the item as men's or women's if(mensRb.selected) { summaryTxt.text += mensRb.label + " "; } else { summaryTxt.text += womensRb.label + " "; } summaryTxt.text += sizeCb.selectedItem.label + " "; switch(itemCp.hexValue) { // Adds the color choice case "990000" : summaryTxt.text += "Burgundy "; break; case "333366" : summaryTxt.text += "Navy "; break; case "336633" : summaryTxt.text += "Forest "; break; default : summaryTxt.text += "Black "; } summaryTxt.text += itemDescTxt.text + "\n"; summaryTxt.text += "Total: " + String(qtyNs.value) + " x 259.95 + "; summaryTxt.text += shipCb.selectedItem.data + " (Shipping) = $"; total = qtyNs.value * 259.95 + Number(shipCb.selectedItem.data); summaryTxt.text += String(total.toFixed(2)); // Shows the checkout summary summaryTxt.visible = true; } stop();

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.

To get the source files for this example, download SmallShoppingCart.zip at the top of this page.

Where to go from here

For more information about this topic, see Using ActionScript 3.0 Components and the ActionScript 3.0 Language and Components Reference.


Related Flash Quick Starts

  • Using the Button component
  • Using the Label component
  • Creating, populating, and resizing the DataGrid component
  • Customizing and sorting the DataGrid component
  • Filtering and formatting data in the DataGrid component
  • Displaying images with the TileList component

More Like This

  • Creating a simple ActionScript 3 class
  • Displaying images with the TileList component
  • Making user interfaces more accessible
  • Animating with ActionScript 3
  • Display list programming in ActionScript 3
  • Getting started with the ActionScript 3 FLVPlayback component
  • Using the Label component
  • Event handling in ActionScript 3
  • Loading images and Library assets with ActionScript 3
  • Using ActionScript 3 drawing commands

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