Home Segments Top Top Previous Next

854: Mainline

Next, you must wire together your new model, an instance of MovieData, and your new view, an instance of JList.

To begin, you define an observer. When activated, the observer fetches the vector of movies from the model, generates a vector of strings from the titles in the vector of movies, and relays the vector of strings to the choice list. The choice list setter, setListData, conveniently takes as its argument a vector of strings.

import java.util.*; 
import javax.swing.*;                            
public class MovieDataObserver implements Observer {  
 private MovieApplication applet; 
 public MovieDataObserver (MovieApplication a) { 
  applet = a; 
 } 
 public void update (Observable observable, Object object) { 
  Vector titles = new Vector(); 
  for (Iterator i = applet.getMovieData().getMovieVector().iterator(); 
       i.hasNext();) {  
   Movie movie = (Movie) (i.next()); 
   titles.add(movie.getTitle()); 
  } 
  applet.getJList().setListData(titles); 
 } 
}