March 1, 2009, 9:39 a.m.
posted by pythonrocks
How to Write an Item ListenerItem events are fired by components that implement the ItemSelectable interface.[15] Generally, ItemSelectable components maintain on/off state for one or more items. The Swing components that fire item events include check boxes, check box menu items, and combo boxes.
Here's some item event-handling code taken from ComponentEventDemo.java:[16]
//Where initialization occurs:
checkbox.addItemListener(this);
...
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
label.setVisible(true);
...
} else {
label.setVisible(false);
}
}
The Item Listener APIFigure lists the methods in the ItemListener interface and Figure describes the methods in the ItemEvent class. Because ItemListener has only one method, it has no corresponding adapter class. Also refer to the API documentation for the ItemListener interface online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ItemListener.html. The API documentation for ItemEvent is online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ItemEvent.html.
Examples That Use Item ListenersThe following table lists some examples that use item listeners.
|
- Comment