|
|
||||||||
| Ultrashock Tutorials > Flash MX 2004 > Flash MX 2004 UI Components | ||||||||
|
||||||||
|
|
Flash MX 2004 UI Components |
|
||||||
06. The List Box family of ComponentsThere are three components that fall into the List Box family of components and are very similar in the way that they work. These are the List Box, Combo Box and the Grid component. Instead of creating separate examples for each of them, let's examine them together in a running example that includes all three. In doing so, you will see the similarities between them and also the different ways in which to populate and use them. Use the following screenshot as a guide when laying out your example Flash application.
Combo BoxThe ComboBox component is one of the easiest components to use. It allows you to display a list of data but does not take up as much space on screen as a ListBox component. Unlike a ListBox component, which can default to not having any of its items selected, the first item in a ComboBox is selected by default. The first item in a combo box is usually a prompt for the user to select one of the other items (e.g. "Please select your country...") Without further ado, let's jump in and see the simplest way to use a ComboBox. In this example, you will be setting the labels of the ComboBox manually using the Property Inspector (PI). In the example with the ListBox, later, you will see how to do this through code.
In this simple example, you set up a listener for the ComboBox to listen for change events. Change events get fired whenever the user changes the selected item in the Combo Box. The event handler gets called with an Event Object that has a target property that points to the ComboBox instance that the event originated from. You can get the selectedItem object for this ComboBox instance using its selectedItem property. The selectedItem object, in turn, has data and label properties. The handler ends by tracing out the label of the selected item to the Output window. Although easy to use, the ComboBox is also quite powerful and contains a myriad of events and methods for doing anything from responding to the Enter key being pressed on a ComboBox instance to when the user rolls over (as opposed to selects) an item within the ComboBox. You may be wondering why I grouped the ComboBox along with the List and Grid components since, in appearance at least, it is very different. The reason is because the ComboBox actually contains a List Box -- the drop down that appears is none other than an instance of the List Box component, which you will be seeing next. List BoxIn the Combo Box example, above, you saw how to populate a list of labels manually, using the Property Inspector. Although it is definitely possible to do things this way (and perhaps even desirable for quick and dirty applications), more often than not you will be receiving this data from the server side and will need to populate a Combo Box or List Box dynamically at runtime. In this example, I will show you how to do this using the List Box.
The code in this example is pretty much self-descriptive. First you create the four items as objects with label and data properties and then use the addItem() method of the List Box component to add them to the component. Finally, you create a listener for the change event, just as you did for the Combo Box example and trace out the cost of the currently selected item.
The GridThe most complex of the the three List Box family of components is the Grid. You can think of a Grid as a multi-column list box. In fact, this is exactly how the component was architected. Unlike the List Box and Combo Box components, which can only have label and data properties in their data sources, the Grid can have any number of custom columns. In this example you will see how to set up a three column grid.
Notice, if you will, how similar the code in this example is to the code in the List Box component example. The difference is that the item objects for the Grid contain custom properties (Product, Price and Quantity) instead of label and data. Also notice how we can query the selected item in a Grid directly to learn the values of its properties. The trace in the change() event handler is broken up over several lines to make it easier to see the message being constructed. Finally, note that the Grid automatically creates your header fields for you using the names of the properties in your items. This is actually a mixed blessing: For the names to display correctly, you had to use capital letters to start the names of the properties. This is actually bad practice. Property names, like method names should start with a lowercase letter. There is a way to display a column name in the header of the grid that is independent of the name of the property for that column, but it is messy. Here's one way of doing it:
If you haven't been following along in Flash, you can find the completed FLA with all three of the previous examples, combo_list_grid.fla, in your downloads. You can play with a slightly modified version, below, where I've replaced the traces with messages displayed in a TextArea component. (To see how to do this, see the exercises in the Check Box and Radio Button sections.)
|
||||||||
©2003 Ultrashock.com - All rights reserved |