[英]JTable Refresh using TableModel
I have been trying to refresh my Jtable but I think I'm somehow getting confused with the tableModel and Table Model listener concepts. 我一直在尝试刷新我的Jtable,但我认为我在某种程度上与tableModel和Table Model侦听器概念混淆了。
So I construct a table by passing a datastructure in the TableModel class 所以我通过在TableModel类中传递数据结构来构造表
public class TasksTableModel extends AbstractTableModel {
int[] collect;
String[] columnNames;
Object[][] data;
public TasksTableModel(int[] collect) {
this.collect=collect;
columnNames=new String[]{"Job","Task"};
for(int i=0;i<10;i++) {
data[i][0]=collect[0];
data[i][1]=collect[1];
}
}
}
public int getRowCount() {
return data.length;
}
public int getColumnCount() {
return columnNames.length;
}
public Object getValueAt(int rowIndex, int columnIndex) {
return data[rowIndex][columnIndex];
}
public String getColumnName(int col) {
return columnNames[col];
}
}
My table is created in another class like this TasksTableModel model = new TasksTableModel(tc); tcTable = new JTable(model);
我的表是在另一个类(如
TasksTableModel model = new TasksTableModel(tc); tcTable = new JTable(model);
TasksTableModel model = new TasksTableModel(tc); tcTable = new JTable(model);
So there is an update to my collect[] object after which i need to refresh the table.So far,I have been setting the table to a new TaskTableModel to refresh where I lose any sorting on existing table.How should I go about doing so. 因此,对我的collect []对象进行了更新,之后我需要刷新表。到目前为止,我一直在将表设置为新的TaskTableModel,以刷新在现有表上失去任何排序的位置。我该怎么做所以。 Please provide a sample code for my better understanding.
请提供示例代码,以使我更好地理解。
Refactor the TasksTableModel
to separate setting the model from the constructor, for example: 重构
TasksTableModel
以将设置模型与构造函数分开,例如:
public TasksTableModel(int[] collect) {
columnNames=new String[]{"Job","Task"};
setModel(collect);
}
public void setModel(int[] collect) {
this.collect=collect;
for(int i=0;i<10;i++) {
data[i][0]=collect[0];
data[i][1]=collect[1];
}
}
Then when you want to update the collect[]
object, do: 然后,当您要更新
collect[]
对象时,请执行以下操作:
model.setModel(collect);
model.fireTableDataChanged();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.