ListSelectionListener

The ListSelectionListener interface is used to handle list selection events. These occur whenever the selection in a list or table has changed. Note that since the ListSelectionListener is a Swing and not an AWT listener interface, to abbreviate class names, the program should include the following statement:

import javax.swing.event.*;

The ListSelectionListener interface consists of a single method, valueChanged(ListSelection-Event e), where e is an object of type ListSelectionEvent. The method javax.swing.event. ListSelectionEvent.getSource obtains the source object generating the event. The value-Changed example uses the javax.swing.JList.getSelectedValue to determine the list item that has been selected.


valueChanged

public void valueChanged(ListSelectionEvent e)
{
if (e.getSource() instanceof JList)
{
if (shoppingList.getSelectedValue().equals(''ice axe"))
{
System.out.println("ice axe selected");
}
}
}

No comments:

Post a Comment