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

static int DESELECTED

This value is returned by the get StateChange method when an item in the list is deselected.

static int SELECTED

This value is returned by the get StateChange method when an item in the list is selected.

Methods

Method

Description

object getItem()

Returns the item that was selected or deselected

object getSource()

Returns the object on which the event occurred

int getStateChange()

Returns either SELECTED or DESELECTED to indicate whether the item was selected or deselected

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 ...

Get Java For Dummies Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.