Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up


Writing an Item Listener

Item events are generated by components that implement the ItemSelectable(in the API reference documentation) interface. 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(in the Creating a User Interface trail), checkbox menu items(in the Creating a User Interface trail), choices(in the Creating a User Interface trail), and lists(in the Creating a User Interface trail).

Item Event Methods

The ItemListener(in the API reference documentation) interface 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

The following applet demonstrates item events. [describe applet]

[applet goes here]


Try this:
  1. Do something.

You can find the applet's code [nowhere yet]. 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 ItemEvent Class

Each item event method has a single parameter: an ItemEvent(in the API reference documentation) object. The ItemEvent class defines the following handy methods:
Object getItem()
Returns the component-specific Object associated with the item whose state changed. Often this is a String containing the text on the selected item. Other possibilities might be an Image or 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 ItemEvent class defines two states: SELECTED and DESELECTED.


Previous | Next | Trail Map | To 1.1 -- And Beyond! | GUI Changes: The AWT Grows Up