JDK-8193442 : Removing TreeItem from a TreeTableView sometime changes selectedItem
  • Type: Bug
  • Component: javafx
  • Sub-Component: controls
  • Affected Version: 8,9,10
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2017-12-12
  • Updated: 2022-06-29
  • Resolved: 2022-04-22
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
openjfx17.0.4Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.8.0_152"
Java(TM) SE Runtime Environment (build 1.8.0_152-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.152-b16, mixed mode)


ADDITIONAL OS VERSION INFORMATION :
Linux asus14 4.4.0-103-generic #126-Ubuntu SMP Mon Dec 4 16:23:28 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux


A DESCRIPTION OF THE PROBLEM :
Removing one TreeItem from a TreeTableView changes TreeTableView selectedItem.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the following javafx program:
----------------------------------------------------
package sample;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        TreeItem<String> rootNode = new TreeItem<String>("Root");
        rootNode.setExpanded(true);
        for (int i = 0; i < 3; i++) {
            rootNode.getChildren().add(new TreeItem<>("Node " + i));
        }
        for (int i = 0; i < 2; i++) {
            TreeItem<String> node = rootNode.getChildren().get(i);
            node.setExpanded(true);
            for (int j = 0; j < 2; j++) {
                node.getChildren().add(new TreeItem<>("Sub Node " + i + "-" + j));
            }
        }

        TreeTableColumn<String, String> column = new TreeTableColumn<>("Nodes");
        column.setCellValueFactory((TreeTableColumn.CellDataFeatures<String, String> p) -> {
            return new ReadOnlyStringWrapper(p.getValue().getValue());
        });
        column.setPrefWidth(200);

        TreeTableView<String> table = new TreeTableView<>(rootNode);
        table.getColumns().add(column);

        int selectIndex = 4; // select "Node 1"
        int removeIndex = 2; // remove "Node 2"
        table.getSelectionModel().select(selectIndex);
        System.out.println("Selected index = " + table.getSelectionModel().getSelectedIndex());
        System.out.println("Selected item  = " + table.getSelectionModel().getSelectedItem().getValue());
        table.getRoot().getChildren().remove(removeIndex);
        System.out.println("Selected index = " + table.getSelectionModel().getSelectedIndex());
        System.out.println("Selected item  = " + table.getSelectionModel().getSelectedItem().getValue());

        primaryStage.setTitle("Tree Table View Selection");
        primaryStage.setScene(new Scene(table, 300, 275));
        primaryStage.show();
    }


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Selected index = 4
Selected item  = Node 1
Selected index = 4
Selected item  = Node 1
ACTUAL -
Selected index = 4
Selected item  = Node 1
Selected index = 3
Selected item  = Sub Node 0-1

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
package sample;

import javafx.application.Application;
import javafx.beans.property.ReadOnlyStringWrapper;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        TreeItem<String> rootNode = new TreeItem<String>("Root");
        rootNode.setExpanded(true);
        for (int i = 0; i < 3; i++) {
            rootNode.getChildren().add(new TreeItem<>("Node " + i));
        }
        for (int i = 0; i < 2; i++) {
            TreeItem<String> node = rootNode.getChildren().get(i);
            node.setExpanded(true);
            for (int j = 0; j < 2; j++) {
                node.getChildren().add(new TreeItem<>("Sub Node " + i + "-" + j));
            }
        }

        TreeTableColumn<String, String> column = new TreeTableColumn<>("Nodes");
        column.setCellValueFactory((TreeTableColumn.CellDataFeatures<String, String> p) -> {
            return new ReadOnlyStringWrapper(p.getValue().getValue());
        });
        column.setPrefWidth(200);

        TreeTableView<String> table = new TreeTableView<>(rootNode);
        table.getColumns().add(column);

        int selectIndex = 4; // select "Node 1"
        int removeIndex = 2; // remove "Node 2"
        table.getSelectionModel().select(selectIndex);
        System.out.println("Selected index = " + table.getSelectionModel().getSelectedIndex());
        System.out.println("Selected item  = " + table.getSelectionModel().getSelectedItem().getValue());
        table.getRoot().getChildren().remove(removeIndex);
        System.out.println("Selected index = " + table.getSelectionModel().getSelectedIndex());
        System.out.println("Selected item  = " + table.getSelectionModel().getSelectedItem().getValue());

        primaryStage.setTitle("Tree Table View Selection");
        primaryStage.setScene(new Scene(table, 300, 275));
        primaryStage.show();
    }

---------- END SOURCE ----------


Comments
A pull request was submitted for review. URL: https://git.openjdk.org/jfx17u/pull/68 Date: 2022-06-29 08:00:55 +0000
29-06-2022

Changeset: 3bb2db12 Author: Jose Pereda <jpereda@openjdk.org> Date: 2022-04-22 19:26:07 +0000 URL: https://git.openjdk.java.net/jfx/commit/3bb2db12a4ae3fe4a26420f0af57c189b2549edb
22-04-2022

A pull request was submitted for review. URL: https://git.openjdk.java.net/jfx/pull/753 Date: 2022-03-14 14:49:41 +0000
14-03-2022

It is reproducible on 8GA as well.
14-12-2017

might be related to JDK-8187596
13-12-2017

Issue is reproducible in both Windows 10 and Ubuntu 16.04.2 LTS. 8u40 : Fail 8u152: Fail 9.0.1+11 : Fail 10-ea+35 : Fail When i changed value of 'selectIndex' to 1 or 2, there is no issue - 'selectedItem' value is correct. So, this issue occurs for some TreeItem nodes.
13-12-2017