'JavaFX TableView doesn't keep the data sorted

I have the following table:

private TableView<CashflowSetForCategory> table;

and a data model:

 private final ObservableList<CashflowSetForCategory> cashflowsByCategory
            = observableArrayList(cashflows -> new Observable[]{
            cashflows.cashflowsProperty(),
            cashflows.cashflowsProperty().sizeProperty(),
            cashflows.totalIncomeProperty(),
            cashflows.totalSpendingsProperty(),
            cashflows.totalBalanceProperty()});

which is bound to the table:

table.setItems(cashflowsByCategory);

Since I have defined an extractor for every property of the CashflowSetForCategory type (these properties basically reflect the columns I have in the table), I would have expected that the table is always notified of any updates in my data and always keeps the correct sort order automatically, even when the data changes.

Indeed the table registers all updates to the data, because it always shows the correct values when something changes in the data. But when I sort the table by a column (which works initially), and then the property that this column relates to changes in the data model, the sort order is not automatically re-evaluated, so the values are no longer correctly sorted:

enter image description here

FYI, updating the data in the model is done by updating the values of the properties, and yes they are observable properties, so my CashflowSetForCategory class has fields like:

private final SimpleObjectProperty<BigDecimal> totalBalance = new SimpleObjectProperty<>(ZERO);

I tried to modify my code using this, which I found during my research:

SortedList<CashflowSetForCategory> cashflowsByCategoryAsSortedList =
     new SortedList<>(cashflowsByCategory);
cashflowsByCategoryAsSortedList.comparatorProperty().bind(table.comparatorProperty());
table.setItems(cashflowsByCategory);

However, it didn't fix the issue.

Why is the table not keeping the data sorted when it changes?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source