Quick Introduction: Flash ComboBox and DataGrid Components
In this Quick Tip on the Flash Professional Components we will be taking a look at the ComboBox and the DataGrid.
Brief Overview
In the demo SWF you'll see a ComboBox and a DataGrid. Choosing a state from the ComboBox will cause a Label to display that state's population and load the state's flag. Choosing a row in the DataGrid will navigate to the chosen site.
Step 1: Setting Up The Document
Open a new Flash Document and set the following properties.
- Document Size: 550x400px
- Background Color: #FFFFFF (white)
Step 2: Add Components to the Stage
Open the components window by going to Window > Components or pressing CTRL+F7.
Drag a ComboBox, a DataGrid, and two Labels to the stage.

In the Properties panel give the ComboBox the instance name "statesCombo".
If the Properties panel is not showing go to Window > Components or press CTRL+F3

Set the ComboBox's X to 20.00 and its Y to 39.00

In the Properties panel give the DataGrid the instance name "sitesDG".
Set the DataGrids X to 20.00 and its Y to 236.00.
In the Properties panel give the first Label the instance name "statesLabel".
Set the Label's X to 200.00 and its Y to 39.00.
In the Properties panel give the second Label the instance name "sitesLabel".
Set this Label's X to 20.00 and its Y to 209.00.
Step 3: Importing the Classes
Create a new ActionScript file and give it the name Main.as
We will be declaring our components in Main.as so we need to turn off auto declare stage instances; the benefit of doing this is that you'll get code hinting for the instance.
Go to File > Publish Settings
Click on "Settings" next to "Script [Actionscript 3.0]".



Uncheck "Automatically declare stage instances".



In Main.as open the package declaration and import the classes we will be using
Add the following to Main.as.
1 |
|
2 |
package { |
3 |
|
4 |
import flash.display.MovieClip; |
5 |
//needed to display images
|
6 |
import flash.display.Loader; |
7 |
//our onstage Components
|
8 |
import fl.data.DataProvider; |
9 |
import fl.controls.ComboBox; |
10 |
import fl.controls.Label; |
11 |
import fl.controls.DataGrid; |
12 |
import flash.events.Event; |
13 |
//needed to autosize textfields
|
14 |
import flash.text.TextFieldAutoSize; |
15 |
import flash.net.URLRequest; |
16 |
import flash.net.navigateToURL; |
Step 4: Set up the Main Class
Add the class declaration itself, extend MovieClip and set up our constructor function. More info on document classes is available here.
Add the following to Main.as.
1 |
|
2 |
public class Main extends MovieClip{ |
3 |
|
4 |
public var statesCombo:ComboBox; |
5 |
public var statesLabel:Label; |
6 |
public var sitesDG:DataGrid; |
7 |
public var sitesLabel:Label; |
8 |
|
9 |
var comboDP:DataProvider; |
10 |
var DataGridDP:DataProvider; |
11 |
|
12 |
var flagLoader:Loader; |
13 |
|
14 |
public function Main() { |
15 |
setupComboDP(); |
16 |
setupDataGridDP(); |
17 |
setupStatesCombo(); |
18 |
setupLabels(); |
19 |
setupSitesDataGrid(); |
20 |
|
21 |
}
|
Step 5: Functions in the Main Constructor
Here we define the setupComboDP, setupDataGridDP, setupStatesCombo, setupLabels, and setupSitesDataGrid functions.
DataProviders provide an easy way to setup data to be provided to components.
In the setupStatesCombo we also add a loader to the stage to load pictures of the flags; we could have defined a separate function to set up the loader, but here we just do it inside this function.
Add the following to Main.as.
1 |
|
2 |
private function setupComboDP():void{ |
3 |
comboDP = new DataProvider(); |
4 |
comboDP.addItem({ Label:"Alabama",population:"4661900" }); |
5 |
comboDP.addItem({ Label:"Alaska" ,population:"686293" }); |
6 |
comboDP.addItem({ Label:"Arizona",population:"6500180" }); |
7 |
comboDP.addItem({ Label:"Arkansas",population:"2855390" }); |
8 |
comboDP.addItem({ Label:"California",population:"36756666" }); |
9 |
comboDP.addItem({ Label:"Colorado",population:"4939456" }); |
10 |
comboDP.addItem({ Label:"Conneticut",population:"3501252" }); |
11 |
comboDP.addItem({ Label:"Delaware",population:"873092" }); |
12 |
comboDP.addItem({ Label:"Florida",population:"18328340" }); |
13 |
comboDP.addItem({ Label:"Georgia",population:"9685744" }); |
14 |
comboDP.addItem({ Label:"Hawaii",population:"1288198" }); |
15 |
comboDP.addItem({ Label:"Idaho",population:"1523816" }); |
16 |
comboDP.addItem({ Label:"Illinois",population:"12901563" }); |
17 |
comboDP.addItem({ Label:"Indiana",population:"6376792" }); |
18 |
comboDP.addItem({ Label:"Iowa",population:"3002555" }); |
19 |
}
|
20 |
|
21 |
private function setupDataGridDP():void{ |
22 |
DataGridDP = new DataProvider(); |
23 |
//adds Items to corresponding colums in the DataGrid
|
24 |
DataGridDP.addItem({site:"Activetuts", description:"Flash Tutorials", address:"https://code.tutsplus.com/categories/flash"}); |
25 |
DataGridDP.addItem({site:"Nettuts", description:"Various Web Design Tutorials", address:"https://code.tutsplus.com"}); |
26 |
DataGridDP.addItem({site:"Mobiletuts", description:"Mobile Device Tutorials", address:"https://code.tutsplus.com"}); |
27 |
DataGridDP.addItem({site:"Psdtuts", description:"PhotoShop Tutorials", address:"https://design.tutsplus.com"}); |
28 |
DataGridDP.addItem({site:"Vectortuts", description:"Vector Program Tutorials", address:"https://design.tutsplus.com"}); |
29 |
DataGridDP.addItem({site:"Aetuts", description:"After Effects Tutorials", address:"https://cgi.tutsplus.com"}); |
30 |
DataGridDP.addItem({site:"Phototuts", description:"Photography Tutorials", address:"https://photography.tutsplus.com"}); |
31 |
}
|
32 |
private function setupStatesCombo():void{ |
33 |
statesCombo.width = 150; |
34 |
statesCombo.prompt = "Choose a State"; |
35 |
statesCombo.dataProvider = comboDP; |
36 |
flagLoader = new Loader(); |
37 |
flagLoader.x = 200.00; |
38 |
flagLoader.y = 60.00; |
39 |
addChild(flagLoader); |
40 |
statesCombo.addEventListener(Event.CHANGE,loadData); |
41 |
}
|
42 |
|
43 |
public function setupLabels():void{ |
44 |
statesLabel.text=""; |
45 |
statesLabel.autoSize = statesLabel.autoSize = TextFieldAutoSize.LEFT; |
46 |
sitesLabel.text="Click on row to visit site"; |
47 |
sitesLabel.autoSize = sitesLabel.autoSize = TextFieldAutoSize.LEFT; |
48 |
|
49 |
|
50 |
}
|
51 |
|
52 |
public function setupSitesDataGrid():void{ |
53 |
//Colums are put into an array here we have 3 columns
|
54 |
sitesDG.columns =["site","description","address"]; |
55 |
sitesDG.dataProvider = DataGridDP; |
56 |
sitesDG.width = 500; |
57 |
sitesDG.addEventListener(Event.CHANGE,gotoSite); |
58 |
}
|
Step 6: Event Listeners
Here we code our Event Listeners.
We get the selected item's Label and show the population for the corresponding state.
We load the corresponding image by converting the selectedItem
(state) to lowercase and appending ".jpg" to it.
Add the following to Main.as.
1 |
|
2 |
public function loadData(e:Event):void { |
3 |
//Get the selectedItems Label e.g. "Alabama"
|
4 |
//Load a relevent .jpg e.g. "alabama.jpg" we convert the selected item (state) to lowercase
|
5 |
statesLabel.text = e.target.selectedItem.Label + "'s population is " + e.target.selectedItem.population; |
6 |
flagLoader.load(new URLRequest("flagsLarge/"+e.target.selectedItem.Label.toLowerCase()+".jpg")); |
7 |
}
|
8 |
|
9 |
public function gotoSite(e:Event):void{ |
10 |
navigateToURL(new URLRequest(e.target.selectedItem.address)); |
11 |
|
12 |
}
|
13 |
}//Close out the class |
14 |
|
15 |
}//Close out the package |
Conclusion
Using The ComboBox and DataGrid components are a good way to display a list of data to choose from.
You'll notice in the components parameters panel of the components that you can check and select certain properties.

The above image is for the ComboBox Component.
The properties are as follows for the ComboBox component.
- dataProvider: the data model of the list of items to be viewed
- editable: a Boolean value that indicates whether the ComboBox component is editable or read-only
- enabled: a Boolean value that indicates whether the component can accept user input
- prompt: the prompt for the ComboBox component.
- restrict: the characters that a user can enter in the text field.
- rowCount: the maximum number of rows that can appear in a drop-down list that does not have a scroll bar.
- visible: a Boolean value that indicates whether the component instance is visible
The properties for the DataGrid are as follows.
- allowMultipleSelection: a Boolean value that indicates whether more than one list item can be selected at a time
- editable: a Boolean value that indicates whether the DataGrid component is editable or read-only
- headerHeight: the height of the DataGrid header, in pixels.
- horizontalLineScrollSize: "a value that describes the amount of content to be scrolled, horizontally, when a scroll arrow is clicked.
- horizontalPageScrollSize: sets the count of pixels by which to move the scroll thumb on the horizontal scroll bar when the scroll bar track is pressed.
- horizontalScrollPolicy: a Boolean value that indicates whether the horizontal scroll bar is always on.
- resizableColumns: Indicates whether the user can change the size of the columns.
- rowHeight: the height of each row in the DataGrid component, in pixels.
- showHeaders: a Boolean value that indicates whether the DataGrid component shows column headers.
- sortableColums: Indicates whether the user can sort the items in the data provider by clicking on a column header cell.
- verticalLineScrollSize: a value that describes how many pixels to scroll vertically when a scroll arrow is clicked.
- verticalPageScrollSize: the count of pixels by which to move the scroll thumb on the vertical scroll bar when the scroll bar track is pressed.
- verticalScrollPolicy: a value that indicates the state of the vertical scroll bar
The help files are a great place to learn more about these properties.
To learn more about the properties for Labels be sure to check out the Quick Tip on the Button and Label components.
To learn how to load the DataGrid from an xml file check out my tutorial on Datgrid with XML.
Thanks again to http://www.state-flags-usa.com for letting me use their flag images. And thanks to you for reading - look out for more Component Introductions!.