如果您想通知您的 JTable 您的数据更改,请在您的 tablemodel 上调用一个方法,该方法将更新数据并调用fireTableDataChanged()
使用 JTableModel 在 JTable 中维护数据是一种很好的做法。
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
那就是您的 tablemodel 将触发fireTableDataChanged()
when 值将发生变化。
class MyTableModel extends AbstractTableModel {
private String[] columnNames = ...//same as before...
private Object[][] data = ...//same as before...
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
/*
* Don't need to implement this method unless your table's
* editable.
*/
public boolean isCellEditable(int row, int col) {
//Note that the data/cell address is constant,
//no matter where the cell appears onscreen.
if (col < 2) {
return false;
} else {
return true;
}
}
/*
* Don't need to implement this method unless your table's
* data can change.
*/
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
...
}