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 box was changed.

Remember.eps When the user changes the selected item in a list control, two item events are raised: one to indicate that the previously selected item has been deselected, and the other to indicate that a new item has been selected. You can use the getState Change and getItem methods to retrieve the item that was selected or deselected. For example:

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

}

}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset