java 设置 JTable 中的列顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4165562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 05:08:10  来源:igfitidea点击:

Set order of columns in JTable

javajtable

提问by Tobias

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

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

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.

现在我想对列重新排序。我知道 JTable 类中有一个名为 moveColumn(A, B) 的函数。这会将一列从 A 移到 B,然后将 B 放在左侧或右侧。我的问题是,我想以特定方式订购整张桌子,我该怎么做?如果我使用 moveColumn,我不知道 B 被移动到哪里,在 10 种情况中的 5 种情况下它可能是正确的一侧,而在其他情况下则是错误的一侧。

Hope you understand my problem :-)

希望你明白我的问题:-)

回答by Guillaume

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

回答by Stephan

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

基于@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]));
    }
  }

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

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

回答by Qwerky

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

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

Extend TableColumnand give your new class a positionproperty. Have it implement Comparableand use the positionto compare columns.

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

Next, extend DefaultTableColumnModeland store TableColumns in an ordered list.

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

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

您的 JTable 现在应该根据它们的position. 未经测试,但听起来很有趣,所以我以后可能会试一试。

回答by camickr

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

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