Sunday, August 2, 2009

JTable Cell Display Based on Data Type

JTable is capable of doing some special rendering based on the data type, such as JCheckBox for boolean data. The supported data type are listed in Sun's Tutorial on JTable. The only restriction is that each column has to be uniformly the same type. To enable this, the getColumnClass() of the JTable's model has to return the type of the data to be displayed:
   DefaultTableModel model = new DefaultTableModel()
* Override to return the data class.
public Class getColumnClass(int c)
return getValueAt(0, c).getClass();


If getColumnClass() is not overridden, then the returned class is Object. In this case, JTable invokes the toString() method of the Object and displayed it as a JLabel.

No comments:

Post a Comment