Using the JFC/Swing Packages |
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. Currently, the only component that fires list selection events is
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
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
TheListSelectionListener
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 aJList
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 inImageChooser.java
. Here's the code that deals with list selection events:Notice that thepublic 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(); } } . . . }valueChanged
method only updates the image ifgetValueIsAdjusting
returnsfalse
. 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: aListSelectionEvent
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 returnstrue
.
Using the JFC/Swing Packages |