Home >
Editor's Note: We've excerpted Chapter 6 from Learning Flex 3 for InsideRIA readers who may be considering giving Flex a try and who need a get-up-to-speed tutorial on handling user input in Flex. Learning Flex 3 is by Alaric Cole, who's contributed open source components to the Flex community and who is a development and consulting specialist at Yahoo!.
Flex makes it easy to create a high level of interactivity. It comes with support for data binding, an excellent event architecture, and a set of components with great user feedback. Add to this the ability to quickly create beautiful effects and transitions, and it's easy to see why Flex is a great tool for developers.
Read the Table of Contents.
Handling user input is what makes an application tick. Dealing with mouse clicks, dragging, typing on the keyboard, and such is where the fun is. Otherwise, you wouldn’t have an application; you’d have just an animation or a static image, and that’s, well, comparatively boring.
However, understanding what someone using your application does and responding to that action takes some planning and work. You have to account for every interaction that you think is important. Basically, you’re thinking about what a user might want to do and preparing for that. Of course, this can be a challenge, but it can be really rewarding as well.
Understanding Events
A Flex application responds to user input by something called an event. An event is something that happened, either by user interaction, or by other things happening such as a photo appearing or data returning from a server. (Getting data from a server is covered briefly in Chapter 10.) When a user clicks a button, for instance, an event occurs. (The event for clicking something is called—you guessed it—click.) And when a button is created, an event also occurs. (This event is called creationComplete.) When the event happens, you say the event fired or was dispatched. To respond to such an event, you set up something called an event handler or listener.
Handling Events Inline
MXML makes listening for events very simple. All you have to do is add the proper event attribute to a tag and tell it what to do. This could mean calling a function or just modifying a property. The following code example shows a Button (myButton) that changes the text of a Label (myLabel), as shown in Figure 6-1, by modifying the Label’s text property. You do this simply by adding the attribute click to the Button:
<mx:Label id="myLabel" text="The Button will change my text" />
<mx:Button id="myButton" label="Change it!" click="myLabel.text = 'Some new text'"/>
The same goes for other events, such as when a TextInput’s text changes by a user typing into the component. That’s a change event, so the following code would cause a TextInput to modify the Label:
<mx:Label id="myLabel" text="The TextInput will change my text" /> <mx:TextInput id="myTextInput" change="myLabel.text =
myTextInput.text"/>
This code modifies the Label’s text to match the TextInput’s text any time the TextInput’s text changes. There’s actually a better way to make a property update itself automatically when another property changes, and that’s called data binding. I’ll go into more detail on that in Chapter 7.
While there’s no limit to the number and names of events that a component can use, you will see a few old standards. Table 6-1 lists the most common types of events.
Table 6-1. The most common events
Of course, tons more events exist, and many are specific to certain controls, but knowing the events in Table 6-1 will get you pretty far. You can see what kinds of events a specific control offers in a few other ways:
- In Design mode, click a control that’s been added to the stage, and look at the Flex Properties panel’s Category view. Check out the Events section in that list, and you’ll see every event available for that component.
- In Source mode, when using code completion on a component, all of its properties will pop up. The events will be listed with a little lightning bolt icon next to them.
- Check out the documentation for a component by selecting Help→Help Contents and searching for a component or by selecting a component in Source mode and then selecting Help→Find in Language Reference. At the top of every component’s documentation page are links to its properties, methods, and events. Check out its list of events for details. (See the sidebar “Reading Flex Documentation” for more information.)
Using Event Constants
A constant is a fixed value in Flex, which means it’s a way to store a value that you don’t expect will change, to make it easier to remember. Constants are usually used to give a name to some numeric value, because, let’s face it, words are easier to remember than numbers. Can you remember π (pi) to the fifteenth decimal place? I didn’t think so. What about the ASCII value for the Caps Lock key? That’s why some smart person invented constants. Get a piece of the pi by using the constant Math.PI, or get the keyboard code for the Caps Lock key with Keyboard.CAPS_LOCK.
But numeric values aren’t all constants are good for. Flex often uses constants for strings as well, and the most common place you’ll see them is in event types. For instance, the event for when the user’s mouse goes out of the Flex application’s bounds is called mouseLeave. Because the string “mouse leave” or “mouse-leave” isn’t sufficient, Flex has a constant set up so you don’t have to remember the specifics: Event.MOUSE_LEAVE.
You might be thinking to yourself, “That’s not any easier to remember!” True, but if it makes you feel any better, because these constants are properties of a class (MOUSE_LEAVE is a property of the Event class, PI is a property of the Math class, and so on.), they’re available using code completion. That means you don’t have to worry about typos. Type the first few letters, and Flex Builder will fill in the rest for you.
You’ve probably noticed by now that constants are all in uppercase—that’s to distinguish them as constants. Though this capitalization is not necessary, it’s considered a best practice. And because it’s difficult to distinguish separate words in all-uppercase text, the standard is to place an underscore ( _ ) between the words.







Recent Comments