java 更新数据库后如何让JTable显示刷新的数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15076301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make JTable show refreshed data after updating database?
提问by Programmer Apprentice
I have updated a database and I would like to refresh a JTable
with this code tied to a button click event, but it doesn't work. When I close aplication and open it again JTable
shows the right data.
我已经更新了一个数据库,我想JTable
用这个与按钮点击事件相关的代码刷新一个,但它不起作用。当我关闭应用程序并再次打开它时会JTable
显示正确的数据。
Do I need to override some method on my abstract table?
我需要覆盖抽象表上的某些方法吗?
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);
}
}
回答by tenorsax
As it is (posted code) the model is not aware of the modification in the database. You could either refresh the data in the model, such as calling existing init()
method or creating a similar one. As an alternative, you can build a new model and reset it on the table.
由于它是(发布的代码),模型不知道数据库中的修改。您可以刷新模型中的数据,例如调用现有init()
方法或创建类似方法。作为替代方案,您可以构建一个新模型并将其重置在桌子上。
It is the responsibility of the model to fire notifications such as fireTableDataChanged
that you use in actionPerformed
. Table clients should not execute these methods. fireTableDataChanged
belongs to init()
method for example. See Firing Data Change Eventsfor more details and examples.
模型有责任触发通知,例如fireTableDataChanged
您在actionPerformed
. 表客户端不应执行这些方法。 例如fireTableDataChanged
属于init()
方法。有关更多详细信息和示例,请参阅触发数据更改事件。
The model should be about data, so it is best if you move the logic of setting textField
out of the model. If needed you can expose additional methods from the model to help clients get additional details about data and state.
模型应该是关于数据的,因此最好将设置逻辑textField
移出模型。如果需要,您可以从模型中公开其他方法,以帮助客户获取有关数据和状态的其他详细信息。
回答by Azam Khan
Tried many things but finally this worked for me:
尝试了很多东西,但最终这对我有用:
if (model.getRowCount() > 0) {
for (int i = model.getRowCount() - 1; i > -1; i--) {
model.removeRow(i);
}
}
setTablevalue();
I removed all the rows from the JTable and again called the setTableValue method to re-populate the table.
我从 JTable 中删除了所有行,并再次调用 setTableValue 方法来重新填充表。