Java Reference
In-Depth Information
public interface TableModel {
// Listeners
public void addTableModelListener(TableModelListener l);
public void removeTableModelListener(TableModelListener l);
// Properties
public int getColumnCount();
public int getRowCount();
// Other methods
public Class getColumnClass(int columnIndex);
public String getColumnName(int columnIndex);
public Object getValueAt(int rowIndex, int columnIndex);
public boolean isCellEditable(int rowIndex, int columnIndex);
public void setValueAt(Object vValue, int rowIndex, int columnIndex);
}
AbstractTableModel Class
The
AbstractTableModel
class provides the basic implementation of the
TableModel
interface.
It manages the
TableModelListener
list and default implementations for several of the
TableModel
methods. When you subclass it, all you need to provide is the actual column and row count,
and the specific values (
getValueAt()
) in the table model. Column names default to labels such
as
A
,
B
,
C
, . . .,
Z
,
AA
,
BB
, . . ., and the data model is read-only unless
isCellEditable()
is overridden.
If you subclass
AbstractTableModel
and make the data model editable, it's your responsi-
bility to call one of the following
fire
XXX
()
methods of
AbstractTableModel
to ensure that any
TableModelListener
objects are notified when the data model changes:
public void fireTableCellUpdated(int row, int column);
public void fireTableChanged(TableModelEvent e);
public void fireTableDataChanged();
public void fireTableRowsDeleted(int firstRow, int lastRow);
public void fireTableRowsInserted(int firstRow, int lastRow);
public void fireTableRowsUpdated(int firstRow, int lastRow);
public void fireTableStructureChanged();
When you want to create a
JTable
, it's not uncommon to subclass
AbstractTableModel
in
order to reuse an existing data structure. This data structure typically comes as the result of a
Java Database Connectivity (JDBC) query, but there's no restriction requiring that to be the
case. To demonstrate, the following anonymous class definition shows how you can treat an
array as an
AbstractTableModel
:
TableModel model = new AbstractTableModel() {
Object rowData[][] = {
{"one", "ichi"},
{"two", "ni"},
{"three", "san"},
{"four", "shi"},
{"five", "go"},
{"six", "roku"},
{"seven", "shichi"},