设置 JTable 中的列顺序

发布于 2024-10-02 11:54:05 字数 497 浏览 1 评论 0原文

我有一个包含一些列的 JTable。我有一个映射到视图中位置的列标识符的 HashMap,例如:

TableHeader1 | TableHeader2 | TableHeader3
    sth.           sth.          sth.

我知道:

TableHeader1 -> position 0
TableHeader2 -> position 1
TableHeader3 -> position 2

现在我想对列重新排序。我知道 JTable 类中有一个名为 moveColumn(A, B) 的函数。这会将一列从 A 移动到 B,并将 B 向左或向右放置。 我的问题是,我想以特定的方式对整个表进行排序,我该怎么做? 如果我使用 moveColumn,我无法知道 B 已移动到哪里,在十分之五的情况下,它可能是右侧,而在其他情况下可能是错误的一侧。

希望你理解我的问题:-)

I have a JTable with some columns. I have a HashMap of the column identifier mapped to the position in the view, for example:

TableHeader1 | TableHeader2 | TableHeader3
    sth.           sth.          sth.

I know that:

TableHeader1 -> position 0
TableHeader2 -> position 1
TableHeader3 -> position 2

Now I want to reorder the columns. I know that there is a function called moveColumn(A, B) within the JTable class. This moves a column from A to B, and B is putted left or right.
My problem is, I want to order the whole table in a specific way, how can I do this?
If I use moveColumn, I cannot know where B has been moved, in 5 out of 10 cases it might be the right side and in the other cases the wrong side.

Hope you understand my problem :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

萌逼全场 2024-10-09 11:54:05

您可以通过删除所有列并按正确的顺序添加它们来更改列顺序:

public static void setColumnOrder(int[] indices, TableColumnModel columnModel) {
    TableColumn column[] = new TableColumn[indices.length];

    for (int i = 0; i < column.length; i++) {
        column[i] = columnModel.getColumn(indices[i]);
    }

    while (columnModel.getColumnCount() > 0) {
        columnModel.removeColumn(columnModel.getColumn(0));
    }

    for (int i = 0; i < column.length; i++) {
        columnModel.addColumn(column[i]);
    }
}

You can change the columns order by removing all of them and adding them in the right order:

public static void setColumnOrder(int[] indices, TableColumnModel columnModel) {
    TableColumn column[] = new TableColumn[indices.length];

    for (int i = 0; i < column.length; i++) {
        column[i] = columnModel.getColumn(indices[i]);
    }

    while (columnModel.getColumnCount() > 0) {
        columnModel.removeColumn(columnModel.getColumn(0));
    }

    for (int i = 0; i < column.length; i++) {
        columnModel.addColumn(column[i]);
    }
}
吻风 2024-10-09 11:54:05

好的,这个怎么样。可能有点左场。

扩展 TableColumn 并为您的新类提供一个 position 属性。让它实现Comparable并使用position来比较列。

接下来,扩展 DefaultTableColumnModel 并将 TableColumn 存储在有序列表中。

您的 JTable 现在应该根据列的位置来显示列。未经测试,但听起来很有趣,所以我稍后可能会尝试一下。

OK how about this. Might be a bit left field.

Extend TableColumn and give your new class a position property. Have it implement Comparable and use the position to compare columns.

Next, extend DefaultTableColumnModel and store TableColumns in an ordered list.

Your JTable should now display columns according to their position. Untested but it sounds interesting so I might give it a go later.

挽你眉间 2024-10-09 11:54:05

根据@Guillaume 的回答,我找到了一种方法来做到这一点,而不需要删除所有列并再次添加它们。

  public static void setColumnOrder(int[] indices, JTable table, TableColumnModel columnModel) {

    for (int i = 0; i < indices.length; i++) {
      columnModel.moveColumn(i, table.convertColumnIndexToView(indices[i]));
    }
  }

这对我来说效果更好,因为使用 (SwingX) JXTable,不可见列的顺序不会被修改。

Based on @Guillaume answer I found a way to do that without the need to remove all columns and add them again.

  public static void setColumnOrder(int[] indices, JTable table, TableColumnModel columnModel) {

    for (int i = 0; i < indices.length; i++) {
      columnModel.moveColumn(i, table.convertColumnIndexToView(indices[i]));
    }
  }

This works better for me because with (SwingX) JXTable, the order of the invisible columns is not modified.

假面具 2024-10-09 11:54:05

这个问题可以通过使用内置的 moveColumn 函数作为冒泡排序的快捷方式来解决。在这种情况下,哈希图保存你的权重。请记住,getColumnModel().getColumn(j).getModelIndex() 可以被视为不会更改的初始列索引/ID。将其视为列标题。

HashMap<int, int> mapOrder = new HashMap<int, int>();

for (int i = 0; i < jt_table.getColumnCount(); i++) {
    for (int j = 0; j < jt_table.getColumnCount() - 1; j++) {
        int col1 = mapOrder.get(jt_table.getColumnModel().getColumn(j).getModelIndex());
        int col2 = mapOrder.get(jt_table.getColumnModel().getColumn(j + 1).getModelIndex());
        if (col1 > col2) {
            jt_table.moveColumn(jt_table.getColumnModel().getColumn(j).getModelIndex(), jt_table.getColumnModel().getColumn(j + 1).getModelIndex());
        }
    }
}

This problem can be solved by using the built in moveColumn function as a bubble sort shortcut. The hashmap holds your weights in that case. Keep in mind that getColumnModel().getColumn(j).getModelIndex() can be seen as the initial column index / ID that will not change. See it as a column title.

HashMap<int, int> mapOrder = new HashMap<int, int>();

for (int i = 0; i < jt_table.getColumnCount(); i++) {
    for (int j = 0; j < jt_table.getColumnCount() - 1; j++) {
        int col1 = mapOrder.get(jt_table.getColumnModel().getColumn(j).getModelIndex());
        int col2 = mapOrder.get(jt_table.getColumnModel().getColumn(j + 1).getModelIndex());
        if (col1 > col2) {
            jt_table.moveColumn(jt_table.getColumnModel().getColumn(j).getModelIndex(), jt_table.getColumnModel().getColumn(j + 1).getModelIndex());
        }
    }
}
千仐 2024-10-09 11:54:05

如果您想按列名称重新排序,则可以查看 表列重新排序建议。

If you want to reorder by column name then you can check out the Table Column Reordering suggestion.

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