[英]JTable not updating added rows by using repaint() method
In my UI I am using a JTable with a TableModel.In a update method of the implemented Observer interface i am calling the repaint method.The delete operations take place immediately while on adding a row it is not updated,but it is updated while switching through tabs( which calls another method instead of repaint) 在我的UI中,我将JTable与TableModel一起使用。在已实现的Observer接口的更新方法中,我调用repaint方法。在添加行时删除操作会立即进行,但不会更新,但在切换时会更新通过选项卡(它调用另一种方法而不是重新绘制)
This is code for update method: 这是更新方法的代码:
public void update(Observable o, Object arg) {
((MyTableModel)table_.getModel()).addTableRow(row);
//addTable(row) adds the row to the dataVector that populates the JTable
//the dataVector is updated with added row
table_.repaint();
}
I wanted to know why the JTable is not getting updated Please Note->the data Vector has the required rows including the added row 我想知道为什么JTable无法更新,请注意->数据向量具有所需的行,包括添加的行
Your addTableRow
method should also fire an event indicating the row has been added to alert the JTable
of this change. 您的
addTableRow
方法还应该触发一个事件,指示已添加该行以警告JTable
此更改。 Then there is no need for the repaint
call. 这样就不需要
repaint
调用。
Take for example a look at the implementation of the addRow
method of the DefaultTableModel
: 例如看一下
DefaultTableModel
的addRow
方法的实现:
public void insertRow(int row, Vector rowData) {
dataVector.insertElementAt(rowData, row);
justifyRows(row, row+1);
fireTableRowsInserted(row, row);
}
You can clearly see that an event is fired, and no repaint needed. 您可以清楚地看到触发了一个事件,不需要重绘。 This is all explained in more detail in the
JTable
tutorial JTable
教程中对此进行了更详细的说明。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.