我有一个jtable,如果我将表数据设置为公共变量(不要将它放在tablemodel的参数上),一切正常.但是现在我需要将表数据设置为参数,表格不会刷新其内容.我打印出一些单元格值和行号,它们都更新和正确.只是显示没有改变,仍然是旧的内容.我一直在尝试所有类型的方法来刷新表,包括fireTableDataChanged和repaint,以及其他fire函数,它们都不起作用.请帮忙.非常感谢!!!!!!
表模型是:
public class MyTableModel extends AbstractTableModel {
protected String[] columnNames;
private String[][] data;
public MyTableModel(String[] columnNames, String[][] data){
this.columnNames = columnNames;
this.data = data;
//I add this here but it still doesn't refresh the table
fireTableDataChanged();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public String getValueAt(int row, int col) {
return data[row][col];
}
public void setData(String[][] newdata) {
data = newdata;
fireTableDataChanged();
}
}
Run Code Online (Sandbox Code Playgroud)
我更新表模型和表的代码是:
tableModel=new MyTableModel(columnNames, data);
tableModel.fireTableDataChanged();
table = new JTable();
table.setModel(tableModel);
((AbstractTableModel) table.getModel()).fireTableDataChanged();
//outputs showed the content is updated
System.out.println("total row count" + table.getRowCount());
System.out.println("cell info" + table.getValueAt(0,5));
table.repaint();
feedback.repaint();
this.repaint();
Run Code Online (Sandbox Code Playgroud)
你不应该重新分配你的桌子:
tableModel=new MyTableModel(columnNames, data);
tableModel.fireTableDataChanged(); // not necessary
table = new JTable(); // dont do this, you are removing your handle to the table in your view
table.setModel(tableModel);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9170 次 |
| 最近记录: |