Home Segments Top Top Previous Next

987: Mainline

Now, with the new table subclass defined in Segment 986, you can create a bare-bones application that displays movie information in a table. The application reuses the data model introduced in Segment 850, and connects the data in that data model to a table by way of a new observer defined in Segment 988. The application and the new observer provide the infrastructure you need to test variations of the table and the table model.

import javax.swing.*; 
import javax.swing.table.*; 
import java.awt.event.*; 
import java.util.*; 
public class MovieTableApplication extends JApplet { 
 // Declare instance variables: 
 private RatingTable ratingTable; 
 private MovieData movieData;                                    
 // Define constructor 
 public MovieTableApplication() { 
  // Create models 
  getMovieData();                                                
  // Create and connect views to application 
  getContentPane().add("Center", new JScrollPane(getRatingTable()));  
 } 
 // Define getters and setters 
 public MovieData getMovieData () {                              
  if(movieData == null) { 
   setMovieData(new MovieData());}  
  return movieData;                                              
 }                                                               
 public void setMovieData (MovieData m) {                        
  movieData = m;                                                 
  movieData.addObserver(new MovieDataObserverForTable(this));                    
  movieData.changed();                                                   
 }                                                               
 public RatingTable getRatingTable () {                          
  if (ratingTable == null) { 
   Vector rows = new Vector(); 
   Vector columns = new Vector(); 
   // Prepare column labels 
   columns.add("Title"); columns.add("Rating"); columns.add("Script");  
   columns.add("Acting"); columns.add("Direction"); 
   // Create and assign new table 
   setRatingTable(new RatingTable(rows, columns)); 
  } 
  return ratingTable;  
 }                                                                       
 public void setRatingTable (RatingTable r) {  
  ratingTable = r;                                                       
 }                                                                       
 public static void main (String argv []) { 
  JFrame frame = new JFrame("Movie Data Table"); 
  frame.getContentPane().add("Center", new MovieTableApplication()); 
  frame.setSize(750, 210); 
  frame.addWindowListener(new ApplicationClosingWindowListener()); 
  frame.show(); 
 } 
}