Java Reference
In-Depth Information
public int getColumnCount() {
return columns;
}
public int getRowCount() {
return rows;
}
public String getColumnName(int column) {
return headers[column];
}
public Object getValueAt(int row, int column) {
return lookup.get(new Point(row, column));
}
public void setValueAt(Object value, int row, int column) {
if ((rows < 0) || (columns < 0)) {
throw new IllegalArgumentException("Invalid row/column setting");
}
if ((row < rows) && (column < columns)) {
lookup.put(new Point(row, column), value);
}
}
}
Testing this example involves creating and filling up the model, as follows:
String headers[] = { "English", "Japanese"};
TableModel model = new SparseTableModel(10, headers);
JTable table = new JTable(model);
model.setValueAt("one", 0, 0);
model.setValueAt("ten", 9, 0);
model.setValueAt("roku - \ u516D", 5, 1);
model.setValueAt("hachi - \ u516B", 8, 1);
Listening to JTable Events with a TableModelListener
If you want to dynamically update your table data, you can work with a TableModelListener to
find out when the data changes. The interface consists of one method that tells you when the
table data changes.
public interface TableModelListener extends EventListener {
public void tableChanged(TableModelEvent e);
}
After the TableModelListener is notified, you can ask the TableModelEvent for the type of
event that happened and the range of rows and columns affected. Table 18-7 shows the prop-
erties of the TableModelEvent you can inquire about.
Search WWH ::




Custom Search