0

I'm developing a small game that has a highscore list displayed by a 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.

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. Of course, it's added to the 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. 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. Each Tab contains a HighscoreTableView with different data taken from a static Data object. The data model is a class HighscoreEntry, an ObservableList of them gets added to the HighscoreTableViews. My full code is available here.

Community
  • 1
  • 1
LukeLR
  • 1,129
  • 1
  • 14
  • 27
  • I believe for the sorting to work the list in the TableView's `items` property must be a `SortedList` with the `comparator` property bound to the one of the TableView. This is presumably the case with the initial value, but you reset it. Try changing `this.setItems(list.getObservableList());` to `this.getItems().clear;` followed by `this.getItems().addAll(list);` – Itai Jul 08 '16 at 04:22

1 Answers1

2

You are calling sort() before any data is loaded. 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. Than all changes will be automatically propagated to view.

whitesite
  • 827
  • 4
  • 12