15.3.1.1 Properties
The TableModel interface supports the properties shown in Table 15-9. The columnCount is the number of columns in the data model. This does not have to match the number of columns reported by the column model. Likewise, rowCount is the number of rows in the data model. columnName and columnClass are indexed properties that let you retrieve the name of the column and the class of objects in the column. The name used in the table model is distinct from anything used in the TableColumn class. For both properties, remember that the index refers to the table model, regardless of where the column appears on the screen.
Table 15-9. TableModel properties
Property
Data type
get
is
set
Default value
columnCount
int
·
rowCount
int
·
15.3.1.2 Events
As you may have come to expect from other models in the Swing package, the TableModel has its own event type, TableModelEvent, generated whenever the table changes. A full discussion of the TableModelEvent class and the TableModelListener appears later in this chapter.
public void addTableModelListener(TableModelListener l) public void removeTableModelListener(TableModelListener l)Add or remove listeners interested in receiving table model events.
15.3.1.3 Cell methods
These methods let you obtain and change the values of individual cells:
-
public Object getValueAt(int rowIndex, int columnIndex)
-
Return the value of the cell at (rowIndex, columnIndex). Base types (int, float, etc.) are wrapped in an appropriate Object.
public boolean isCellEditable(int rowIndex, int columnIndex)
-
Return true if the cell at (rowIndex, columnIndex) can be edited.
public void setValueAt(Object aValue, int rowIndex, int columnIndex)
-
Set the value of the cell at (rowIndex, columnIndex) to aValue. As with the getValueAt( ) method, you may need to wrap primitive data types in an Object (like Integer) before using them to set the value of a cell.