Home Segments Top Top Previous Next

985: Mainline

For example, the instance of the DefaultTableModel class produced when you construct a JTable instance determines that the cells in the table are editable, but editable cells are not useful unless you connect appropriate listeners to the table; you learn about such listeners later, in Segment 1011.

For the moment, assume that your table's cells are not to be edited. You define a subclass of the DefaultTableModel class in which you define a shadowing version of the isCellEditable method that always returns false. Machinery defined in the JTable class calls that function to decide whether a cell should be editable.

The DefaultTableModel subclass also needs a two-parameter constructor—for a vector of row vectors and for a column vector—that passes both arguments to the corresponding constructor in the DefaultTableModel class.

import java.util.*; 
import javax.swing.table.*; 
public class RatingTableModel extends DefaultTableModel { 
 public RatingTableModel (Vector rows, Vector columns) { 
  super(rows, columns); 
 } 
 public boolean isCellEditable(int row, int column) { 
  return false; 
 } 
}