Home Segments Top Top Previous Next

855: Mainline

Now, you define a listener class for your choice list. The choice list will activate your listener by calling an interface-imposed method, valueChanged, whenever you select with a mouse click an item in the choice list.

Because the storage of strings in the choice list parallels the storage of movies in the MovieData instance, the index used by getMovieData will fetch the movie from the MovieData instance that corresponds to the selection from the choice list, determined by getSelectedIndex:

import javax.swing.*; 
import javax.swing.event.*; 
public class MovieListListener implements ListSelectionListener {          
 private MovieApplication applet; 
 public MovieListListener (MovieApplication a) { 
  applet = a; 
 } 
 public void valueChanged(ListSelectionEvent e) {               
  int index = applet.getJList().getSelectedIndex(); 
  applet.setMovie(applet.getMovieData().getMovie(index)); 
 } 
}