0

我正在从事一个涉及 JTable 并对其执行排序和过滤操作的项目。我完成了排序和过滤部分,现在我希望能够从旧表的当前视图创建一个新表。
例如,如果我对旧表应用某些过滤器,则会过滤掉一些行。我不希望在我的新表中过滤掉那些行。我想我可以将新的行索引转换为模型索引并将单元格值手动添加到新表的模型中,但我想知道是否还有其他有效的方法可以做到这一点?
以下是我最终做的事情:

//this code block will print out the rows in current view
int newRowCount = table.getRowCount();
int newColumnCount = table.getColumnCount();
for (int i = 0; i < newRowCount; i++) {
    for (int j = 0; j < newColumnCount; j++) {
    int viewIndex = table.convertRowIndexToModel(i);
    String value = (String) model.getValueAt(viewIndex, j);
    System.out.print(value + "\t");

    }
    System.out.println();

}
4

1 回答 1

1

不需要任何索引转换,只需查询表而不是底层模型

for (int i = 0; i < table.getRowCount(); i++) {
    for (int j = 0; j < table.getColumnCount(); j++) {
        Object value = table.getValueAt(i, j);
        System.out.print(value + "\t");
    }
}

注意:为了便于阅读,最好将 i/j 重命名为行/列,太懒了 ;-)

于 2011-05-09T10:29:17.473 回答