我定义了一个如下所示的自定义表格模型
class TModel extends AbstractTableModel {
Object col[] = null;
Object[][] data = null;
public void setCollen(Object[] col) {
this.col = col;
}
public void setObj(Object[][] oo) {
this.data = oo;
}
public int getColumnCount() {
return col.length;
}
public int getRowCount() {
return data.length;
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public String getColumnName(int column) {
return (String) col[column];
}
public boolean isCellEditable(int row, int col) {
return false;
}
public void setValueAt(Object aValue, int row, int column) {
data[row][column] = aValue;
}
public void clear() {
data = null;
}
}
然后我定义了一个这样的表
TModel tableModel = new TModel();
JTable table = new JTable(tableModel);
现在我想使用方法table.setRowSorter()
对表的数据进行排序。
我试过这样写代码
table.setRowSorter(new TableRowSorter(tableModel));
显然是错误的。
请帮帮我。
最佳答案
你的
AbstractTableModel
是错误的,未完成,缺少重要的通知 f.e.fireTableCellUpdated(row, col);
在setValueAt
,即....对于基本的排序功能,您可以定义
table.setAutoCreateRowSorter(true)
只有我建议使用
DefaultTableModel
, 数据结构基于Object[][]
或Vector<Vector>
,没有必要重写所有通知程序是否已正确实现
关于java - 如何为自定义表格模型设置行排序器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13736141/