【问题标题】:How to display empty rows in a JTable and be able to sort the empty rows to always appear at bottom如何在 JTable 中显示空行并能够对空行进行排序以始终显示在底部
【发布时间】:2016-12-03 20:30:53
【问题描述】:

我有一个带有一些数据和一些空行的 JTable。 该表由具有 5 行数据的表模型构成。但是我通过重载getRowCount() 以在表模型中返回 11 来显示它并在 5 个填充行下方增加 6 个空行。

但是,当我进行排序时,空行将出现在表格的顶部。我希望空行始终显示在表格底部。

在不创建第二个表且在可排序表下方添加空行的情况下,最好的方法是什么?

谢谢!

【问题讨论】:

标签: java swing jtable


【解决方案1】:

创建一个进行所需排序的TableRowSorter,并使用JTable.setRowSorter 将其注册为排序器。

这将涉及创建一个Comparator,根据某些或所有字段,空行总是大于非空行。如果您想让空行出现在底部,即使您颠倒排序顺序,这也必须考虑SortOrder。您可以通过RowSorter.getSortKeys获得订购。

【讨论】:

    【解决方案2】:
    The call to getSortKeys().get() must use 0 (i.e. the main sort key) and not column:
    
    boolean ascending = getSortKeys().get(0).getSortOrder() == SortOrder.ASCENDING;
    If the user clicks, for example, over the column number 12 or 7, you want the first sort key (there will be 3 at most, if the user clicked before over other columns, to perform multi column sort). This sort key will have two attributes:
    
    Sort Order (ASCENDING, DESCENDING, UNORDERED)
    getSortKeys().get(0).getSortOrder()
    Column Index (12 or 7, for example)
    getSortKeys().get(0).getColumn()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多