|
|
Using Components, the GUI Building Blocks |
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)- Called by the AWT just after a state change in the listened-to component.
Examples of Handling Item Events
Here is some item-event handling code taken fromComponentEventDemo.java:public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { label.setVisible(true); } else { label.setVisible(false); } }You can find more examples of item listeners in the following source files:
The
ItemEventClassEach item event method has a single parameter: anItemEventobject. The
ItemEventclass defines the following handy methods:
Object getItem()- Returns the component-specific object associated with the item whose state changed. Often this is a
Stringcontaining the text on the selected item. For an item event generated by aList, this is anIntegerthat specifies the index of the selected item.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.
|
|
Using Components, the GUI Building Blocks |