从当前 JTable 视图创建新的 JTable
我正在开发一个涉及 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();
}
I am working on a project that involves JTable and performing sorting and filtering operations on it. I am done with the sorting and filtering part and now I want to be able to create a new table from the current view of older table.
e.g. If I apply certain filters to my old table, some rows are filtered out. I don't want those filtered out rows in my new table. I figured that I can convert the new row indices to model indices and add the cell values manually to new table's model, but I was wondering if there's any other efficient way to do this?
Following is what I ended up doing:
//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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不需要任何索引转换,只需查询表而不是底层模型
注意:最好将 i/j 重命名为行/列以提高可读性,太懒了;-)
no need for any index conversion, simply ask query the table instead of the underlying model
Note: better rename the i/j to row/column for readability, was too lazy ;-)