JavaSwingJTable and TableModel: TableModel



Generally, it may be a good idea to throw exceptions if column or row arguments are out of range. Otherwise you will hide bugs.

Class getColumnClass(int column)

The result is only used when the JTable looks up default renderers and editors, which it bases on the column class.

DefaultTableModel always returns Object.class

String getColumnName(int column)

The result is only used when the JTable implicitly creates TableColumns and then is set as the headerValue of the TableColumn.

int getColumnCount()

int getRowCount()

Object getValueAt(int row, int column)

The actual accessor method.

boolean isCellEditable(int row, int column)

It is unclear what to do if this property changes. Maybe you should notify that the content changed (although it didn't).

void setValueAt(Object value, int row, int column)

Should one throw an Exception if the cell isn't editable? Strictly speaking yes, but what if it was editable once and the caller just hasn't noticed that it isn't anymore?

It is reasonable to assume that after the method at least getValueAt(row, column).equals(value); (or that they are both null). This is not the place to convert the value to an internal representation; convert your TableCellEditor to already return the right Object. Should be able to ignore the value?

Notifying of changes / Firing events

The following cases have to be handled by firing a competely change:

The first two aren't that much integrated into JTable, only used in a few methods. The column classes are only used for default renderers and editors.

In case where it actually changes ...

(C) 2001-2009 Christian Kaufhold (