When sorting a TreeTableView, after the sorted content has been cleared, an IndexOutOfBoundsException is thrown.
The following code snippet reproduces the issue:
@Override
public void start(Stage stage) {
TreeTableView<String> treeTableView = new TreeTableView<>();
TreeTableColumn<String, String> treeTableColumn1 = new TreeTableColumn<>("Name");
treeTableColumn1.setCellValueFactory(p -> new SimpleStringProperty(p.getValue().getValue()));
treeTableView.getColumns().add(treeTableColumn1);
TreeItem<String> item1 = new TreeItem<>("Joe");
TreeItem<String> root = new TreeItem<>("root");
root.getChildren().add(item1);
treeTableView.setRoot(root);
treeTableView.setShowRoot(false);
Scene scene = new Scene(treeTableView, 640, 480);
stage.setScene(scene);
stage.show();
// 1. select
treeTableView.getSelectionModel().select(0);
// 2. sort
treeTableView.getSortOrder().add(treeTableColumn1);
treeTableView.sort();
// 3. clear
root.getChildren().clear();
// 4. sort again
treeTableView.sort(); // <-- This causes the exception
}
Throws:
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: [ fromIndex: 0, toIndex: 1, size: 0 ]
at javafx.controls/com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:171)
at javafx.base/javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:243)
at javafx.controls/com.sun.javafx.scene.control.SelectedItemsReadOnlyObservableList.getAddedElements(SelectedItemsReadOnlyObservableList.java:156)
at javafx.controls/com.sun.javafx.scene.control.SelectedItemsReadOnlyObservableList.lambda$new$1(SelectedItemsReadOnlyObservableList.java:65)
at javafx.base/com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
at javafx.base/com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at javafx.base/javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.base/javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.base/javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.base/javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.controls/com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList._endChange(ReadOnlyUnbackedObservableList.java:64)
at javafx.controls/javafx.scene.control.MultipleSelectionModelBase$SelectedIndicesList._endChange(MultipleSelectionModelBase.java:896)
at javafx.controls/javafx.scene.control.ControlUtils.updateSelectedIndices(ControlUtils.java:202)
at javafx.controls/javafx.scene.control.TreeTableView$TreeTableViewArrayListSelectionModel.fireCustomSelectedCellsListChangeEvent(TreeTableView.java:3354)
at javafx.controls/javafx.scene.control.TreeTableView$TreeTableViewArrayListSelectionModel.access$2100(TreeTableView.java:2390)
at javafx.controls/javafx.scene.control.TreeTableView.sort(TreeTableView.java:1899)
It happens in these versions:
JavaFX 15-ea+7+, JavaFX 15+ (possibly after JDK-8193800)
JavaFX 11.0.9+ (possibly after JDK-8252339)