javafx tableview不保留数据排序
我有下表:
private TableView<CashflowSetForCategory> table;
和数据模型:
private final ObservableList<CashflowSetForCategory> cashflowsByCategory
= observableArrayList(cashflows -> new Observable[]{
cashflows.cashflowsProperty(),
cashflows.cashflowsProperty().sizeProperty(),
cashflows.totalIncomeProperty(),
cashflows.totalSpendingsProperty(),
cashflows.totalBalanceProperty()});
绑定到表:
table.setItems(cashflowsByCategory);
由于我已经为CashFlowsetForCategory类型的每个属性定义了一个提取器(这些属性基本上反映了我在表中的列),所以我希望该表总是会通知我的数据中的任何更新,并且即使数据更改,也总是会自动保留正确的排序顺序。
实际上,该表将所有更新都注册到数据中,因为当数据中某些内容变化时,它总是显示正确的值。但是,当我按列对表排序(最初有效),然后该列与数据模型中的更改相关的属性时,排序顺序不会自动重新评估,因此不再正确排序该值:
< a href =“ https://i.sstatic.net/yk9qh.png” rel =“ nofollow noreferrer”>
fyi,更新模型中的数据是通过更新属性的值来完成的,是的,它们是可观察到的属性,因此我的Cash flowsetForCategory类具有:
private final SimpleObjectProperty<BigDecimal> totalBalance = new SimpleObjectProperty<>(ZERO);
我尝试使用此操作来修改代码,我在研究过程中发现:
SortedList<CashflowSetForCategory> cashflowsByCategoryAsSortedList =
new SortedList<>(cashflowsByCategory);
cashflowsByCategoryAsSortedList.comparatorProperty().bind(table.comparatorProperty());
table.setItems(cashflowsByCategory);
但是,它没有解决这个问题。
为什么表更改数据时不将其排序?
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:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论