Home Segments Top Top Previous Next

1007: Mainline

Of course, if you specify a selection mode, you presumably want a listener to take note of selection events. The following illustrates the view side of such a listener. Nothing happens on the model side; the listener only prints the range of the selected rows. Such a method—which extends part way, but not all the way, toward useful work—is called a stub.

The listener implements ListSelectionListener, which requires a valueChanged definition, which is called by the machinery that watches for list-selection events. The list-selection model supplies information about the selected range. The getValueIsAdjusting call ensures that nothing happens until you have completed your selection operation by releasing your mouse button.

import javax.swing.*; 
import javax.swing.event.*; 
public class MovieRowSelectionListener implements ListSelectionListener { 
 private MovieTableApplication application; 
 public MovieRowSelectionListener (MovieTableApplication a) { 
  application = a; 
 } 
 public void valueChanged (ListSelectionEvent e) { 
  if (!(e.getValueIsAdjusting())) { 
   ListSelectionModel lsm  
    = application.getRatingTable().getSelectionModel(); 
   System.out.println("Selected range boundaries are "  
                      + lsm.getMinSelectionIndex()  
                      + " and " 
                      + lsm.getMaxSelectionIndex()); 
  } 
 } 
}