Home Segments Top Top Previous Next

1000: Mainline

Once you have ensured that getColumnClass reports the actual class of the elements in each column, you associate your new renderer with the table and classes for which you have prepared that renderer. You can do the association conveniently in a modification of the RatingTable constructor defined in Segment 992.

To handle instances of both the String and Integer classes, you include two calls to setDefaultRenderer, each with an argument that specifies the class for which the renderer is to be used. You specify a class using the class name followed by a dot and then the keyword class.

import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
public class RatingTable extends JTable {
 public RatingTable (Vector rows, Vector columns) {
  super(new RatingTableModel(rows, columns));
  TableColumnModel model = getColumnModel();
  for (int i = 0; i < columns.size(); ++i) {
   TableColumn column = model.getColumn(i);
   if (i == 0) {
    column.setPreferredWidth(417);
   }
   else {
    column.setPreferredWidth(83);
   }
  }
  setRowSelectionAllowed(false);
  MovieTableCellRenderer renderer = new MovieTableCellRenderer(); 
  setDefaultRenderer(Integer.class, renderer);                    
  setDefaultRenderer(String.class, renderer);                     
 } 
}