|
|
GUI Changes: The AWT Grows Up |
Item events are generated by components that implement theItemSelectableinterface. These are components that maintain state -- generally on/off state for one or more items. The 1.1 AWT components that generate item events are checkboxes
, checkbox menu items
, choices
, and lists
.
Item Event Methods
TheItemListenerinterface has just one method, so it has no corresponding adapter class. Here's the method:
void itemStateChanged(ItemEvent)- Notifies the listener that an item's state has changed. To find which item's state changed, use the
ItemEventgetItemmethod. To find what the new state is, use theItemEventgetStateChangemethod.Examples of Handling Item Events
The following applet demonstrates item events. [describe applet][applet goes here]
Try this:
- Do something.
You can find the applet's code in ItemDemo.java. Here is the applet's item event handling code:
[code goes here]You can find more examples of item listeners in the following sections: [LIST GOES HERE]
The
ItemEventClassEach item event method has a single parameter: anItemEventobject. The
ItemEventclass defines the following handy methods:
Object getItem()- Returns the component-specific
Objectassociated with the item whose state changed. Often this is aStringcontaining the text on the selected item. Other possibilities might be anImageor an object with no visual representation.ItemSelectable getItemSelectable()- Returns the component that generated the item event.
int getStateChange()- Returns the new state of the item. The
ItemEventclass defines two states:SELECTEDandDESELECTED.
|
|
GUI Changes: The AWT Grows Up |