Javafx: Re-sorting a column in a TableView

前端 未结 8 1765
春和景丽
春和景丽 2020-12-09 05:22

I have a TableView associated to a TreeView. Each time a node in the TreeView is selected, the TableView is refreshed with different data.

I am able to sort any col

相关标签:
8条回答
  • 2020-12-09 05:55

    You can also do this for 0 or more Sort-Columns:

    List<TableColumn<Room, ?>> sortColumns = new LinkedList<>(rooms.getSortOrder());
    // rooms.setItems(...)
    rooms.getSortOrder().addAll(sortColumns);
    

    The reason why you create a new LinkedList is that you don't wanna just point at rooms.getSortOrder() like this:

    List<TableColumn<Room, ?>> sortColumns = rooms.getSortOrder();
    

    because this way both rooms.getSortOrder() and sortColumns will become empty after you call rooms.setItems(...) which seems to clear the rooms.getSortOrder().

    0 讨论(0)
  • 2020-12-09 05:56

    You can also use a SortedList.

    SortedList<MatchTableBean> tableItems = new SortedList<>(
            observableList, Comparator.comparing(MatchTableBean::isMarker).reversed().thenComparing(MatchTableBean::getQueryRT));
        tableItems.comparatorProperty().bind(table.comparatorProperty());
        table.setItems(tableItems);
    

    This way the table is sorted, even when the content changes or is completely replaced.

    0 讨论(0)
提交回复
热议问题