ass*_*ias 4 java javafx javafx-2 javafx-8
我有一个简单的 TableView(Java FX 2.0,但我认为问题很普遍),它获得了默认的排序功能。然而,该表的最后一行有一个总数,所以我想从排序算法中排除最后一行。
我找到了一个 Swing JTable 的解决方案,该解决方案包括为总行创建一个单独的表- 可以转置为 TableView,但它似乎有点麻烦。我曾尝试实现我自己的比较器,但我认为不可能创建一个既能按升序又能按降序工作的比较器。
使用 JavaFX 8 可以定义排序策略,并且问题更容易修复。假设包含总计的行是TOTAL
:
table.sortPolicyProperty().set(t -> {
Comparator<Row> comparator = (r1, r2) ->
r1 == TOTAL ? 1 //TOTAL at the bottom
: r2 == TOTAL ? -1 //TOTAL at the bottom
: t.getComparator() == null ? 0 //no column sorted: don't change order
: t.getComparator().compare(r1, r2); //columns are sorted: sort accordingly
FXCollections.sort(t.getItems(), comparator);
return true;
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2256 次 |
最近记录: |