从当前 JTable 视图创建新的 JTable

发布于 2024-11-06 01:55:00 字数 642 浏览 6 评论 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();

}

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里寻她 2024-11-13 01:55:00

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

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 重命名为行/列以提高可读性,太懒了;-)

no need for any index conversion, simply ask query the table instead of the underlying model

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");
    }
}

Note: better rename the i/j to row/column for readability, was too lazy ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文