[英]How to refresh a table (JTable) without changing the ordering done with setAutoCreateRowSorter?
为了填充JTable,我使用的是AbstractTableModel。 我还提供了使用setAutoCreateRowSorter进行排序的机会。 所有这些操作都插入到计时器进程中,该进程在1分钟后执行数据刷新。 每次刷新数据时如何更改排序?
谢谢
//... Type of **data** is a Matrix -> data[][]
//... Type of **columnName** is an Array -> columnName[]
//... Type of **table** is a JTable
//... Type of **model** is an AbstractTableModel
//...Other code Before
try {
table.setAutoCreateRowSorter(true);
} catch(Exception continuewithNoSort) { }
//...Other code After
Timer timerToRefresh = new Timer(0, new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
//Method that populates the matrix
popolateTable(nList);
} catch (Exception e1) {
e1.printStackTrace();
}
// Popolate the model
model = new DefaultTableModel(data,columnName){
private static final long serialVersionUID = 1L;
public boolean isCellEditable(int row, int column) {
return false;
}
};
// set the model to the table
table.setModel(model);
}
});
timerToRefresh.setDelay(60000); // Refresh every 60 seconds
timerToRefresh.start();
我将分配数组数据包含在新数据中,
好吧,您不能这样做,因为这将创建一个新的TableModel,它将重置排序器。
因此,假设您使用的是DefaultTableModel,则基本逻辑应为:
model.setRowCount(0); // to delete the rows
for (each row of data in the Array)
model.addRow(...);
现在,仅数据将被删除并添加到模型中,因此分类器将保留。
另一个选择是在重新创建TableModel之前保存排序器的状态。 您可以从DefaultRowSorter获取当前的排序键。 因此,基本逻辑是:
有关此方法的示例,请参见: 尝试使表刷新后保留排序器位置 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.