![]() |
![]() |
||||||
|
|||||||
User interface components receive a wide variety of events whenever they are manipulated. Buttons and menu items, for example, receive action events whenever they are selected. Similarly, lists and combo boxes receive item events as each item is browsed. A combo box also gets a separate action event when an item is eventually selected. Event Listeners Events can be handled by attaching one or more event listeners to user interface components. Generally speaking, an event listener is an instance of a user-defined class that implements one or more special-purpose event listener interfaces, such as java.awt.event.ActionListener or java.awt.event.ItemListener. Event listeners are typically implemented as JavaBeans™ classes, and are represented in markup either by a generic object element, or by specialized element types reflecting the type of the event listener. The itemListener element, for example, represents a type of event listener that listens for item events. Handling Item Events The following example shows how to attach a single item listener to the comboBox element. In reality, you may attach any number of listener elements to a single component. The listener elements can be attached in any order, and listener elements of different types may be interleaved freely, but all event listener elements, taken together as a group, usually come right after the reserve and property elements, if any, but before the elements representing the layout manager, sub-components, or items, if any. The following example also introduces a new layout manager called Card Layout that specializes in laying out components stacked on top of each other like a deck of cards. In this particular example, we use a combo box to manipulate the card layout, but you could use any other suitable mechanism for that task. Notice that as you browse items from the combo box, the “card” in the middle changes to match the currently highlighted item, but the combo box at the top and the status bar at the bottom stay where they are. Make this object in eNode Sandbox. The result should be similar to the following: ![]() The Example Explained The item listener is described using an object element whose class attribute is set to the value com.enode.xalt.CardBrowser. We would have preferred to describe the listener using the itemListener element type. But because we have to set the listener’s cards property, we have to use the generic object element type to represent the listener in this case. This is because only the object element type has built-in support for bean properties. The itemListenerRef element pulls the object out of the reserve using the URI reference specified in its href attribute and “typecasts” the object back to an item listener, whence it is added to the combo box. The panel element that was added to the controller object has no affect on the composition of the top-level panel unless it is also added to the panel element representing the top-level panel. The panelRef element immediately following the comboBox element does just that. Because the top-level panel uses border layout, and because the panelRef element does not specify the constraints attribute, the “cards” panel is assumed to be constrained to the center of the top-level panel. The CardBrowser Bean The code for the CardBrowser bean referenced by the itemListener element is shown below. Because it is needed by most applications and can be used as-is, it is included as a standard class in the com.enode.xalt package. |
|||||||
|
Copyright © 2002 eNode, Inc. All Rights Reserved. |