0

I have a javafx TableView and I want to know when the sort order of a column changes. I added a listener to the getSortOrder method. However, it only fires when I sort ascending, and never when I sort descending. To test this I used this example code Does anyone have an idea why this doesn't fire? Do I need to add sth else?

The data in the table is in a SortedList and I added the listener as follows:

personTable.getSortOrder().addListener(this::onColumnSortOrderChanged);

 private void onColumnSortOrderChanged(ListChangeListener.Change<? extends TableColumn<Person, ?>> change) {
    boolean changed = false;
    while (change.next()) {
        changed = true;
    }
    if (changed) {
        if (change.getList().isEmpty()) {
            System.out.println("observable is empty");
        } else {
            TableColumn<Person, ?> column = change.getList().get(0);
            System.out.println("Sorted: " + column.getText() + " sort type " + column.getSortType());
        }
    }
}
4

1 回答 1

2

多列可用于排序(例如,首先按姓氏对人进行排序,然后使用名字作为次要标准)。您可以按住Shift以添加多个列。如果您在列的升序和降序排序之间进行更改,则此列表不会更改。

要收听这些更改,您还需要收听该TableColumn.sortType属性。

// print sort order on change
table.getSortOrder().addListener((Observable o) -> {
    System.out.println("sort order: "+ table.getSortOrder().stream().map(TableColumn::getText).collect(Collectors.joining(", ", "{", "}")));
});

// print sortType on change
column.sortTypeProperty().addListener(o -> System.out.println("sortType changed to: " + column.getSortType()));
于 2018-09-10T10:19:23.483 回答