Home Segments Top Top Previous Next

853: Mainline

On the view side, a choice list is an instance of the JList class, or a subclass of that class. Such instances produce graphical displays of choices on which you can click to make a selection.

There is no need to modify the behavior of the basic JList class; the built-in methods suffice. Accordingly, you do not need to define a subclass; you can install a JList directly into the evolving applet.

import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class MovieApplication extends JApplet {
 // Declare instance variables:
 private Meter meter;
 private JList jList;                                           
 private Movie movie;
 private MovieData movieData;
 // Define constructor
 public MovieApplication() {
  // Create models
  getMovie();
  getMovieData();
  // Create and connect views to application
  getContentPane().add("Center", getMeter());
  getContentPane().add("East", getJList());
 }
 // Define getters and setters
 public Meter getMeter () {
  if (meter == null) {setMeter(new Meter(0, 30));}
  return meter;
 }
 public JList getJList () {                                     
  if (jList == null) {setJList(new JList());}                   
  return jList;                                                 
 }                                                              
 public Movie getMovie () {
  if(movie == null) {setMovie(new Movie (10, 10, 10, "On to Java"));}
  return movie;
 }
 public MovieData getMovieData () {
  if(movieData == null) {setMovieData(new MovieData ());}
  return movieData;
 }
 public void setMeter (Meter m) {
  meter = m;
  meter.addMouseListener(new MeterListener(this));
 }
 public void setMovie (Movie m) {
  if(movie == m) {return;}
  if(movie instanceof Movie) {movie.deleteObservers();}
  if(m instanceof Movie) {
   movie = m;
   movie.addObserver(new MovieObserver(this));
   movie.changed();
 }}
 public void setMovieData (MovieData m) {
  movieData = m;
 }
 public void setJList (JList j) {                               
  jList = j;                                                    
 }                                                              
}