我已经更新了一个数据库,我想JTable
用这个绑定到按钮单击事件的代码来刷新一个,但它不起作用。当我关闭应用程序并再次打开它时,会JTable
显示正确的数据。
我是否需要覆盖抽象表上的某些方法?
try {
model=new MyTableModel(this.database);
table = new JTable(model);
} catch (SQLException e) {
e.printStackTrace();
}
scrollPane = new JScrollPane(table);
refresh.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
((MyTableModel)table.getModel()).fireTableDataChanged();
}
});
class MyTableModel extends AbstractTableModel {
private int i;
private String[] columnNames = { "ID", "Code", "Country", "Radio",
"primljeno", "select" };
private Object[][] data = { { new Integer(0), "", "", "", "",
new Boolean(false) } };
public MyTableModel(My_class_Database database)throws SQLException {
init(database);
}
public void init(My_class_Database database) throws SQLException
{
this.i = database.checkIsDataThere();
Object[][] object;
if (i == 0) {
object = new Object[i][7];
object = database.getDatafromDatabase(this.i);
this.data = object;
dataAp = object;
} else {
object = database.getDatafromDatabase(this.i);
textField.setText("No data in database");
}
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public String getColumnName(int col) {
return columnNames[col];
}
public void setValueAt(Object value, int row, int col) {
data[row][col] = value;
fireTableCellUpdated(row, col);
}
// problem sa null vrijednostima,null exception
public Class getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col > 4) {
return true;
} else
return (false);
}
}