Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages

How to Write a List Selection Listener


Note: This section assumes that you're familiar with the AWT event listener scheme. If you aren't, you can read about it in The New AWT Event Model(in the Creating a User Interface trail)
List selection events occur when the selection in a list is either changing or has just changed. Swing has two kinds of sources for list selection events: components and list selection models(in the API reference documentation). Currently, the only component that fires list selection events is JList.

[PENDING: JTable uses a list selection model, which seems to fire these events. Should we mention it? Should we mention components that generally use JLists in their implementation -- JComboBox, JFileChooser (I think not).]

List Selection Event Methods

The ListSelectionListener(in the API reference documentation) interface has just one method, so it has no corresponding adapter class. Here's the method:
void valueChanged(ListSelectionEvent)
Called when the selection in the listened-to component is changing, as well as just after the selection has changed.

Examples of Handling List Selection Events

How to Use Lists features the demo application pictured below that uses a JList to display the names of images to view.
When you choose an image name from the list, the list fires list selection events, which are handled by the list's listener. You can find the program in ImageChooser.java. Here's the code that deals with list selection events:
public class ImageChooser extends JPanel
                          implements ListSelectionListener {
    //...in the constructor for this class:
    // add this object as a listener for the list
    listOfImages.addListSelectionListener(this);
    . . .
    public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
            JList theList = (JList)e.getSource();
            currentImage = new ImageIcon((String)imageList.elementAt(theList.getSelectedIndex()));
            picture.setIcon(currentImage);
            picture.setPreferredSize(new Dimension(currentImage.getIconWidth(),
                                                   currentImage.getIconHeight()));
            picture.revalidate();
        }
    }   
    . . .
}
Notice that the valueChanged method only updates the image if getValueIsAdjusting returns false. Many list selection events can be generated from a single user action such as a single mouse click. This particular program is interested only in the final result of the user's action.

The ListSelectionEvent Class

Each list selection event method has a single parameter: a ListSelectionEvent(in the API reference documentation) object.

The event object represents the change in selection. One list selection event can indicate a selection change for multiple, contiguous items in the list. The ListSelectionEvent class defines the following handy methods:

int getFirstIndex()
Returns the index of the first item whose selection value may have changed.
int getLastIndex()
Returns the index of the last item whose selection value may have changed.
int getValueIsAdjusting()
Returns true if the selection is still changing. Many list selection listeners are interested only in the final state of the selection and can ignore list selection events when this method returns true.


Previous | Next | Trail Map | Creating a User Interface | Using the JFC/Swing Packages