简体   繁体   English

javafx:默认情况下对TableView排序

[英]javafx: sort TableView by default

I'm developing a small game that has a highscore list displayed by a javafx TableView . 我正在开发一个小型游戏,该游戏具有由javafx TableView显示的高分列表。 I've created a subclass HighscoreTableView extends TableView which is a TableView node that automatically creates the TableColumns I need and fills them with data on construction. 我创建了一个子类HighscoreTableView extends TableView ,它是一个TableView节点,该节点自动创建我需要的TableColumns并用构造数据填充它们。

I want that table to be sorted by a default column on initialisation. 我希望该表在初始化时按默认列排序。 I've added the following code lines: 我添加了以下代码行:

duration.setSortType(TableColumn.SortType.DESCENDING);
this.getSortOrder().add(duration);
duration.setSortable(true);
this.sort();

where duration is the TableColumn that should define the sorting. 其中duration是应该定义排序的TableColumn Of course, it's added to the TableView . 当然,它已添加到TableView But when I create a new instance of that HighscoreTableView , it remains unsorted by default, until the user clicks on one of the column headers. 但是,当我创建该HighscoreTableView的新实例时,默认情况下它将保持未排序状态,直到用户单击列标题之一为止。 This is unexpected, since this question , this question and this question say it should work that way. 这是意外的,因为这个问题这个问题这个问题都说它应该那样工作。 Any ideas? 有任何想法吗?

Further information for reproduction: The HighscoreTableView class is used by a HighscoreStage extends Stage class, which contains a TabPane with four Tabs. 用于再现进一步信息: HighscoreTableView类由一个HighscoreStage extends Stage类,其中包含一个TabPane具有四个标签。 Each Tab contains a HighscoreTableView with different data taken from a static Data object. 每个选项卡都包含一个HighscoreTableView具有从静态Data对象获取的不同数据。 The data model is a class HighscoreEntry , an ObservableList of them gets added to the HighscoreTableView s. 数据模型是HighscoreEntry类,它们的ObservableList被添加到HighscoreTableView My full code is available here . 我的完整代码在这里

You are calling sort() before any data is loaded. 您正在加载任何数据之前调用sort() Instead you should call it each time after adding/changing data to the table view. 相反,您应该在向表视图添加/更改数据之后每次都调用它。 Also you can use SortedList with appropriate comparator to wrap your original backing list. 您也可以将SortedList与适当的比较器一起使用以包装原始后备列表。 Than all changes will be automatically propagated to view. 比所有更改都将自动传播到视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM