Macromedia FLEX-FLEX BUILDER TUTORIALS User Manual

Flex Builder Tutorials
This series of four interconnected tutorials explains how you can build a simple Macromedia Flex application using Macromedia Flex Builder. The application is part of a website for the fictitious Flex Store company. The application gives visitors the ability to view a product catalog, find out more about each product, and add products to a shopping cart. The application is a simplified version of the Flex Store application installed with the Flex server.
The tutorials focus on using the development tools in Flex Builder, not on developing Flex applications. The goal is to show you how to use Flex Builder to develop a Flex application more rapidly and with less coding.
You can complete each tutorial as a stand-alone unit or as a part of a larger four-part tutorial. You don’t have to complete all the tutorials or complete them in order, except for the first tutorial, which you must complete before you can start any of the others.
The first tutorial shows you how to set up a Flex development environment. The second shows you how to lay out a Flex user interface with Flex Builder. The third shows you how to use Flex Builder to create custom components, the building blocks of Flex applications. The fourth shows you how to visually bind components to data with Flex Builder.
This chapter includes the following tutorials:
“Tutorial: Setting up a development environment” on page 1
“Tutorial: Creating a layout with Flex Builder” on page 6
“Tutorial: Building custom components with Flex Builder” on page 15
“Tutorial: Binding components to data with Flex Builder” on page 39

Tutorial: Setting up a development environment

This tutorial shows you how to install the Flex server on your computer, copy sample files to a folder, and define a Flex Builder site. You must complete these tasks to complete the other tutorials in this chapter.
In this tutorial, you’ll accomplish the following tasks:
“Install the Flex server on your computer” on page 2
“Copy the tutorial folders to the Flex samples folder” on page 4
“Define a Flex Builder site” on page 5
1

Install the Flex server on your computer

This part of the tutorial describes how to set up the Developer edition of Flex on the same computer as Flex Builder. To compile and run MXML and ActionScript files, you need access to a Flex server.
The setup described in this part of the tutorial is only one of many possible configurations. You can also install Flex on a remote server running a different J2EE server and access it through a network or using FTP. If you don’t select the integrated JRun4/Flex install option, make sure you deploy the samples .war file. For more information, see the Flex installation guide on the Macromedia website at www.macromedia.com/go/flex_install/.
The Developer edition is for noncommercial use for developing and testing Flex applications. It is not licensed for deployment. After 60 days, it will support requests from only five IP addresses (plus localhost), but you can still use it for development and testing as long as you want. The software does not expire.
1.
Copy the Windows version of the Flex installer from the Macromedia Flex CD.
2.
In Windows, double-click the Flex installer file, flex-10-win.exe.
The install wizard appears:
3.
Follow the onscreen instructions.
4.
When prompted for a serial number, leave the text box blank.
You don’t need to enter a serial number for the Developer edition. The Flex server works for 60 days as a trial edition; after that, it becomes a Developer edition.
2 Flex Builder Tutorials
5.
On the Install Options dialog box, select the Integrated JRun4/Macromedia Flex option.
The dialog box gives you other installation options. This tutorial assumes that you selected the Integrated JRun4/Macromedia Flex option.
6.
After you finish installing Flex 1.0, install any Flex updaters available on the Macromedia website at www.macromedia.com/go/flex_updaters.
7.
Start the server by selecting Start > All Programs > Macromedia > Macromedia Flex > Start Integrated Flex Server.
The server displays startup information.
Caution: Do not close the Start Integrated Flex Server dialog box once the server is running. Closing it shuts down the Flex server.
Tutorial: Setting up a development environment 3
For more information, see the Flex installation documentation on the Macromedia website at
www.macromedia.com/go/flex_install/.
You can verify that the Flex server is running normally as follows:
1.
Open the following URL in your browser:
http://localhost:8700/samples
The Flex Sample Apps page appears.
2.
Select a sample application to run it.
If the application doesn’t run, try the following troubleshooting suggestions:
Problem Possible solution
Port conflicts If you created a new server for the Flex application, make sure the ports used by
that server don’t conflict with other servers running on your web application server.
Application server
Flash Player Make sure you’re running the latest Macromedia Flash Player. Flash Player is
Make sure the integrated JRun4/Flex server is running. In Windows, select Start > All Programs > Macromedia > Macromedia Flex > Start Integrated Flex Server.
installed when you install Flex Builder.

Copy the tutorial folders to the Flex samples folder

In this part of the tutorial, you copy the Flex Builder tutorial folders to the folder called samples on the Flex server. The samples folder is the Flex application root folder of the Flex Sample Apps application.
Caution: You must copy the tutorial folders to the folder called samples or the tutorials will not work properly.
1.
Locate the Tutorial folder in the following folder on your hard disk.
C:\Program Files\Macromedia\Flex Builder\Tutorial\
If you didn’t install Flex Builder to the default location, locate the Tutorial folder on your hard disk.
2.
Select the three folders (fbBindings, fbComponents, and fbLayout) in the Tutorial folder and copy them to the following Flex application root folder:
C:\Program Files\Macromedia\Flex\jrun4\servers\default\samples\
Note: If you installed Flex on a different computer, you must copy the files to the samples folder on the remote computer. For more information, see “Examples and site settings” in Using Flex Builder Help.
4 Flex Builder Tutorials
3.
In the Flex application root folder, locate the folder named products in the \samples\flexstore\assets\ folder and copy it to the \samples\fbBindings\assets\ folder.
The products folder contains the product images used in the tutorial. If you're working with the integrated JRun4/Flex server locally, the path of the original products folder is as follows:
C:\Program Files\Macromedia\Flex\jrun4\servers\default\samples\flexstore\assets\products
After copying the folder, it should also appear at the following location:
C:\Program Files\Macromedia\Flex\jrun4\servers\default\samples\fbBindings\assets\products

Define a Flex Builder site

In this part of the tutorial, you define a Flex Builder site for the tutorials. A Flex Builder site lets you visually design, preview, and debug MXML and ActionScript files without leaving Flex Builder.
After you define the Flex Builder site, you can start working on the tutorials.
This section assumes you installed the Flex server on the same computer as Flex Builder using the integrated JRun/Flex install option. If you installed the Flex server on a different computer, you must use a procedure that’s different from the one below to define the site. For instructions, see “Defining the site if the Flex server is remote” in Using Flex Builder Help.
1.
In Flex Builder, s elect Site > Manag e Sites, click the New button in the Manage Sites dialog box, and select Flex Site from the context menu.
The Flex Server Site Setup dialog box appears.
Tip: You can also open this dialog box by clicking the Flex Site link on the Start page.
2.
In the Site Name text box, enter Flex Builder Samples.
The name identifies your site. The samples folder contains not only the Flex Builder tutorials, but other sample applications as well.
3.
In the Local Root Folder text box, click the folder icon and select the following folder:
C:\Program Files\Macromedia\Flex\jrun4\servers\default\samples\
It should contain the Flex Builder tutorial folders (see “Copy the tutorial folders to the Flex
samples folder” on page 4), among other folders.
This step tells Flex Builder where your files are located on your hard disk.
4.
In the Flex Server Root Folder text box, make sure the following path is specified:
C:\Program Files\Macromedia\Flex\jrun4\servers\default\samples\
This folder must always be a Flex application root folder on the server. You can tell if a folder is an application root folder by checking to see if it has a WEB-INF/flex folder containing a flex­config.xml file. An application root folder always has this folder and file. For more information, see “Identifying a Flex application root folder” in Using Flex Builder Help.
This step tells Flex Builder where it can “test” files—that is, where it can send MXML and ActionScript files to be compiled by the Flex server at design time. Flex Builder needs to get these files compiled so you can preview MXML files, get build and debugging information, and connect to back-end systems.
Tutorial: Setting up a development environment 5
5.
In the URL Prefix text box, enter the following URL:
http://localhost:8700/samples/
Caution: Flex Builder may auto-populate this text box with http://localhost:8700/flex/. Make sure you change the value to http://localhost:8700/samples/.
To get the Flex server to compile files while you work, Flex Builder uploads the file to the application root folder you specified and attempts to request it using the URL prefix.
The Flex Server Site Setup dialog box should look like this:
6.
Click OK to define the site and close the dialog box, and then click Done to close the Manage Sites dialog box.
The Flex Builder site is defined and the tutorials are set up. To start working on a tutorial, see any of the following:
“Tutorial: Creating a layout with Flex Builder” on page 6
“Tutorial: Building custom components with Flex Builder” on page 15
“Tutorial: Binding components to data with Flex Builder” on page 39

Tutorial: Creating a layout with Flex Builder

This tutorial shows you how to use Flex Builder to quickly lay out a Flex user interface.
You can complete this tutorial as a stand-alone unit or as the first part of a multipart tutorial. In either case, you must complete the setup tutorial before you begin. For instructions, see “Tutorial:
Setting up a development environment” on page 1.
In this tutorial, you’ll accomplish the following tasks:
“Review the approved user interface mock-ups” on page 7
“Create an MXML file” on page 8
“Import your CSS styles” on page 9
“Position the page title” on page 9
“Position the catalog component” on page 11
“Position the product detail and shopping cart components” on page 12
“Add view buttons to the product catalog” on page 13
6 Flex Builder Tutorials

Review the approved user interface mock-ups

During the planning and approval stages of the project, members of your team produced final mock-ups of the user interface for the Flex Store. You will use these mock-ups to guide you when you create the MXML layout.
The final mock-ups for the Flex Store application are stored in your fbLayout folder:
/fbLayout/mockups
You can open a mock-up file in your favorite image editor from the Files panel in Flex Builder. This feature is useful if you want to quickly change or touch up a mock-up.
1.
In Flex Builder, make sure the Flex Builder Samples site is selected in the Files panel.
If the Files panel is closed, select Window > Files to open it.
2.
In the Files panel, locate the uiGrid.png image and double-click it.
Flex Builder opens the image in your default image editor. For more information on setting a default image editor, see “Setting external image editor preferences” in Using Dreamweaver Help.
The first mock-up file, uiGrid.png, shows the general layout of the Flex Store user interface:
Tutorial: Creating a layout with Flex Builder 7
The user will also have the option of viewing thumbnails in the product catalog on the left side of the layout. The second mock-up file, uiThumbs.png, shows this thumbnail view:
The layout contains the following custom components:
Two interchangeable product catalog components on the left side, which give the user two
views of the catalog
A product detail component on the upper right side
A shopping cart component on the lower right side
Another tutorial shows you how to build these components (see “Tutorial: Building custom
components with Flex Builder” on page 15). In this tutorial you create the basic layout of the
application.

Create an MXML file

In this part of the tutorial, you take the first step in laying out the application by creating an MXML file. MXML is a XML-based language for building Flex applications. For more information, see “About MXML files” in Using Flex Builder Help.
1.
In Flex Builder, make sure the Flex Builder Samples site is selected in the Files panel.
2.
Select File > New.
The New Document dialog box appears.
3.
Select Flex Development in the left pane and MXML Application in the right pane, and then click Create.
The dialog box closes and a blank MXML file appears.
8 Flex Builder Tutorials
4.
Set the following property in the Attributes panel of the Tag inspector (Window > Tag Inspector):
Styles > verticalGap: 0
Note: The angle bracket means the verticalGap property is located in the Styles category of the Attributes tab. This convention is used throughout the tutorials.
5.
Save the file in the fbLayout folder by selecting File > Save, double-clicking the fbLayout folder, and naming the file as follows:
flexstore.mxml

Import your CSS styles

In this part of the tutorial, you import CSS styles into the flexstore.mxml file. You want to import styles defined in an external CSS file to make sure your site has a consistent look and feel. CSS styles also give you more flexibility when you want to change the design of your site.
1.
Make sure the flexstore.mxml file is open in Flex Builder.
2.
Open the CSS Styles panel (Window > CSS Styles) and click the Attach Style Sheet button at the lower edge of the panel.
The Attach External Style Sheet dialog box appears.
3.
In the Attach External Style Sheet dialog box, click Browse to select the external CSS style sheet called flexstore.css located in the fbLayout folder.
After you click OK twice to close the dialog boxes, the name of the external CSS style sheet appears in the CSS Styles panel. You can click the Plus (+) icon to display a tree view of all the styles in the style sheet and their properties.
4.
Save your work.

Position the page title

After importing the CSS styles, the next task is to create a basic layout for the user interface. In this part of the tutorial, you position the page title (Flex Store) in the upper left corner of the layout, as indicated in the mock-up of the user interface (see “Review the approved user interface
mock-ups” on page 7).
You decide to use a VBox (for vertical box) container to position the page title at the top of the file. The rest of the user interface will be positioned below the page title.
Tutorial: Creating a layout with Flex Builder 9
1.
In Design view, click the Expanded button on the Document toolbar and then click anywhere inside the Application container.
Expanded mode adds borders and padding to controls and containers in Design view to help you lay out your application. (The borders and padding appear only at design time.) You can click the Standard button at any time to get a better representation of how your project will look after it’s compiled.
2.
In the Containers category of the Insert bar, click the VBox button.
Flex Builder inserts a VBox container in the file.
3.
With the insertion point still blinking in the VBox container, set the following property in the Attributes panel:
Size > widthFlex: 0
Tip: To verify that the component is selected, check that <mx:VBox> and its associated icon appear at the top of the Attributes panel.
4.
Insert the page title by clicking anywhere inside the VBox container and clicking the Label button in the Controls category of the Insert bar.
Flex Builder inserts a Label control in the file and selects it.
5.
Modify the Label text by double-clicking the Label control to open the Quick Tag Editor, and then changing the
6.
With the Label control still selected, set the following property in the Attributes panel:
Common > styleName: appTitle
text property value to "Flex Store" as follows and pressing Enter.
You can select the style from the pop-up menu that appears when you click the field for the
styleName property.
You can see the effect of applying the style in Design view.
7.
Save your work.
10 Flex Builder Tutorials

Position the catalog component

In this part of the tutorial, you position the product catalog on the left side of the user interface as shown in the mock-ups (see “Review the approved user interface mock-ups” on page 7). To meet this design requirement, you decide to use an HBox (for horizontal box) container to position the two halves of your user interface side by side.
1.
In Code view (View > Code), place the insertion point at the lower edge of the VBox container by clicking immediately before the closing
2.
In the Containers category of the Insert bar, click the HBox button.
</mx:VBox> tag.
Flex Builder inserts an HBox container in the file.
3.
Click anywhere in the <mx:HBox> tag, click the Refresh button on the Attributes panel, and set the following properties in the panel:
Styles > horizontalGap: 4
Size > height: 548
Size > width: 860
Note: When you set the first size property, a message box appears warning you that the container clips any content that extends beyond the specified size. Select the Don’t Show Me This Message Again option, and click OK.
4.
Switch to Design view (View > Design), click anywhere in the HBox container, and click the Panel button in the Containers category of the Insert bar.
Flex Builder inserts a Panel container in the file. You want to use a Panel container for the product catalog.
5.
Specify the panel’s title by double-clicking the Panel container to open the Quick Tag Editor, and then entering the property
title="Product Catalog" as follows.
6.
With the Quick Tag Editor still open, enter the following properties and then Press Enter:
id="main" height="544" width="478"
Tip: Use code hints to work more rapidly.
7.
Insert a ViewStack container inside the Panel container by clicking anywhere inside the Panel container, clicking the ViewStack button on the Insert bar, and entering bodyStack in the ID text box in the dialog box that appears.
After you click OK, Flex Builder inserts a ViewStack container in the Panel container. You want to use this container to place two views of the product catalog—a grid view and a thumbnail view—in the same space.
8.
With the ViewStack container still selected, set the following property in the Attributes panel:
Effects > changeEffect: Fade
You can select the Fade value from the pop-up menu. The Fade effect will make the ViewStack container change from transparent to opaque in 500 milliseconds (the default effect duration).
9.
Save your work.
Tutorial: Creating a layout with Flex Builder 11

Position the product detail and shopping cart components

In this part of the tutorial, you position the product detail and shopping cart components on the right side of the user interface as shown in the mock-ups (see “Review the approved user interface
mock-ups” on page 7). You decide to use the existing HBox container to meet this layout
requirement.
The mock-up also shows the product detail and shopping cart components stacked on top of each other. To meet this layout requirement, you decide to use a VBox container.
1.
In Design view, click inside the HBox container to the right of the Panel container.
Be careful not to click inside the Panel container; the vertical insertion bar should appear beside the Panel container.
2.
In the Containers category of the Insert bar, click the VBox button.
Flex Builder inserts a VBox container in the file. You want to use the VBox container to stack the product detail and shopping cart components on top of each other.
3.
With the insertion point still blinking in the VBox container, set the following property in the Attributes panel:
Size > widthFlex: 1
You want the VBox container to scale based on its content.
4.
Click anywhere in the new VBox container, click the Canvas button in the Containers category of the Insert bar, and accept the default width and height values in the dialog box that appears.
Flex Builder inserts a Canvas container in the VBox. You want to insert the product detail component in this container.
5.
With the Canvas container still selected, set the following properties in the Attributes panel:
Common > id: topCanvas
Size > height: 326
Size > width: 364
Size > widthFlex: 1
Other > vScrollPolicy: off
Tip: If you prefer, you can list all the properties alphabetically by clicking the Show List View button on the Attributes panel.
6.
Insert another Canvas container by switching to Code view, clicking immediately after the closing
</mx:Canvas> tag, and clicking the Canvas button on the Insert bar.
After you accept the default width and height values in the dialog box that appears, Flex Builder inserts another Canvas container in the VBox. You want to insert the shopping cart component in this container.
12 Flex Builder Tutorials
7.
Click anywhere in the new <mx:Canvas> tag, click the Refresh button on the Attributes tab, and set the following properties in the Attributes tab:
Common > id: bottomCanvas
Size > height: 208
Size > width: 364
Size > widthFlex: 1
Other > vScrollPolicy: off
8.
Switch back to Design view to inspect your layout.
Tip: Press F4 to hide the workspace and maximize the Document window. Press F4 again to restore the workspace.
9.
Save your work.

Add view buttons to the product catalog

The product catalog area of the layout gives the user two views of the products—a grid view and a thumbnail view. According to the user interface mock-ups, the user should be able to switch views by clicking view buttons at the lower edge of the catalog. For information about the user interface mock-ups, see “Review the approved user interface mock-ups” on page 7.
After studying the mock-up, you decide to use the following containers to lay out the buttons:
A ControlBar container in the left Panel container to create a footer
An HBox container in the ControlBar container to position the buttons horizontally
You use the following steps to lay out and insert the buttons with Flex Builder.
1.
Make sure the flexstore.mxml file is open in Flex Builder.
2.
Insert a ControlBar container by clicking inside the lower edge of the Panel container (without clicking inside the ViewStack container), and then clicking the ControlBar button in the Containers category of the Insert bar.
Flex Builder inserts an empty ControlBar container and automatically positions it at the lower edge of the Panel container.
3.
With the insertion point still blinking in the ControlBar container, specify the values for the following properties in the Attributes panel:
Size > height: 45
Styles > horizontalAlign: right
4.
Insert an HBox container by clicking anywhere inside the ControlBar container, and clicking the HBox button on the Insert bar.
5.
With the insertion point still blinking in the HBox container, set the value for the following property in the Attributes panel:
Styles > horizontalGap: 0
Tutorial: Creating a layout with Flex Builder 13
6.
Insert the thumbnail view button by clicking inside the HBox container, clicking the Image button in the Controls category of the Insert bar, and selecting the following image file:
/fbLayout/assets/images/thumb_off.png
7.
Insert the grid view button by clicking inside the HBox container on the right side of the thumbnail view button (without selecting the button), clicking the Image button on the Insert bar, and selecting the following image file:
/fbLayout/assets/images/list_off.png
8.
Save your work.
In Design view, the completed layout should look similar to the following figure if you select the Application container:
This completes the layout tutorial. If you like, you can continue building the Flex Store by completing the components tutorial, which shows you how to build custom components and insert them in your layout.
14 Flex Builder Tutorials

Tutorial: Building custom components with Flex Builder

In this tutorial, you learn how to use Flex Builder to create custom components, the building blocks of Flex applications. The Flex Store application requires the following custom components:
Two catalog components that give the user different views of the product catalog
A product detail component that gives the user more detail on a product the user clicks in
the catalog
A shopping cart component that lists the products the user wants to purchase
You can complete this tutorial as a stand-alone unit or as the second part of a multipart tutorial. In either case, you must complete the setup tutorial before you begin. For instructions, see
“Tutorial: Setting up a development environment” on page 1.
The tutorial includes a pre-built set of files so you can complete the tutorial without completing the layout tutorial first. If you completed the layout tutorial, you can overwrite the files in the fbComponents folder with your files in the fbLayout folder.
In this tutorial, you will accomplish the following tasks:
“Build the grid view component” on page 16
“Build the thumbnail view component” on page 19
“Build the product detail component” on page 21
“Build the shopping cart component” on page 28
“Insert the view components in the Flex Store layout” on page 32
“Insert the detail and cart components in the layout” on page 34
“Activate the catalog view buttons” on page 35
Tutorial: Building custom components with Flex Builder 15

Build the grid view component

According to the following mock-up, the grid view component should display the Flex Store product catalog as a two-column grid of names and prices. The user can select the grid view by clicking a button at the lower edge of the catalog.
The grid view component does not contain any product data in this tutorial. Another tutorial describes how to add data (see “Tutorial: Binding components to data with Flex Builder”
on page 39).
1.
In Flex Builder, make sure the Flex Builder Samples site is selected in the Files panel.
2.
Select File > New.
The New Document dialog box appears.
3.
Select Flex Development in the left pane and MXML Component:Vertical in the right pane, and then click Create.
The dialog box closes and a component file with a VBox container appears. By default, Flex Builder assigns the value of 400 to the
height and width properties. You don’t want to use
these values.
16 Flex Builder Tutorials
4.
With the insertion point still blinking in the VBox container, specify the values for the following properties in the Attributes panel:
Size > height: Clear the value
Size > width: Clear the value
Styles > verticalGap: 0
Note: The “Size > height” expression means the height property is located in the Size category of the Attributes tab. This convention is used throughout the tutorials.
5.
Insert a DataGrid component to display the product catalog by clicking anywhere in the VBox container, and clicking the DataGrid button in the Controls category of the Insert bar.
The DataGrid dialog box appears.
6.
Set the dialog box options as follows:
ID: list
Header Text (first row): Name
Header Text (second row): Price
Column Name (first row): name
Column Name (second row): price
The DataGrid dialog box should look similar to the following figure:
Caution: Make sure the column name values exactly match “name” and “price.” Column names must exactly match the name of the data fields that will be assigned to the DataGrid in the bindings tutorial.
7.
Click OK.
Flex Builder inserts a DataGrid component in your component file.
8.
Click the DataGrid component and set the following properties in the Attributes tab:
Size > heightFlex: 1
Size > widthFlex: 1
Tutorial: Building custom components with Flex Builder 17
Loading...
+ 39 hidden pages