JDK-8256283 : IndexOutOfBoundsException when sorting a TreeTableView
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8u271,openjfx11,openjfx15,openjfx16
  • Priority: P2
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2020-11-12
  • Updated: 2021-03-02
  • Resolved: 2021-01-29
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
openjfx11.0.11Fixed
Related Reports
Relates :  
Relates :  
Description
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)

Comments
Changeset: db6941de Author: Ambarish Rapte <arapte@openjdk.org> Date: 2021-01-29 17:12:33 +0000 URL: https://git.openjdk.java.net/jfx/commit/db6941de
29-01-2021

The root cause of this issue is that the selection is not cleared when the selected item is removed. There are several such known scenarios with selection that are collected under an umbrella task JDK-8248217. A proper fix to this problem would be to correct the selection issues, which would be a big change and may affect MultipleSelectionModelBase class, which is parent to almost all selection model classes. This would need more time to fix. Hence we are planning to only fix this exception with a trivial patch. Fix is to skip the sorting when rootItem has no children.
22-01-2021

It looks like this also affects JDK 8u271, but since the test program fails and throws an exception back to the application as far back as 8u60, this new exception in 8u isn't a serious problem. It was broken on 8u before this bug was introduced and will still be broken after it is fixed.
13-11-2020

Looks like regression of JDK-8193800.
13-11-2020