ItemEvent Class
Package: java.awt
An instance of the ItemEvent
class is passed to the ItemListener
whenever the item selected in a list control (such as a list box or combo box) is changed. You can use this object to determine information about the event such as which item the user selected.
Fields
Field |
Description |
|
This value is returned by the |
|
This value is returned by the |
Methods
Method |
Description |
|
Returns the item that was selected or deselected |
|
Returns the object on which the event occurred |
|
Returns either |
You can use the getSource
method to determine which component sourced the event when the listener is registered as an event listener with more than one component. For example:
private class MyItemListener
implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == listBox1)
{
// code to handle listBox1 changed
}
if (e.getSource() == listBox2)
{
// code to handle listBox2 changed
}
}
}
In this example, the private class MyItemListener
can be registered with two list boxes (listBox1
and listBox2
). The getSource
method is used in the itemStateChanged
method to determine which list box was changed.
public void itemStateChanged(ItemEvent e)
{
String item = (String)(e.getItem());
if (e.getStateChange ==
ItemEvent.DESELECTED)
}
// code to handle an item being
// deselected
}
else if (e.getStateChange ==
ItemEvent.SELECTED)
{
// code to handle an item being
// selected
}
}