Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8093584

Listener on selected items list within MultipleSelectionModel (TreeView) reports not existing state



    • Bug
    • Resolution: Fixed
    • P4
    • 8u40
    • 8u5
    • javafx


      I noticed that listener on selected items within TreeView calls 'onChanged' with strange state.
      This problem appears when javafx.scene.control.SelectionMode.MULTIPLE is used.

      I attached sample application which allows to reproduce problem.
      Case 1:
      - select items: 'Item 1' and 'Item 2'
      - collapse node: 'Root Node 1'

      output >>
      [TreeItem [ value: Root Node 1 ]]

      Listener added to observable list: MultipleSelectionModel.getSelectedItems() is called three times. First call contains one 'null' element selected, I don't know why. Selected elements are obtained using ListChangeListener.Change.getList() which, according to the JavaDoc can be always called.

      Case 2 - I suppose it may be another form of first case:
      - select items: 'Item 21' and 'Item 22'
      - collapse node 'Item 2'

      [TreeItem [ value: Root Node 2 ]]
      [TreeItem [ value: Item 2 ]]

      In this case, 'null' was replaced by TreeItem 'Root Node 2'. Listener informs that this item was selected, but it wasn't in this case.

      Sample application:

      package treeSelectionProblem;

      import javafx.application.Application;
      import javafx.collections.ListChangeListener;
      import javafx.scene.Node;
      import javafx.scene.Scene;
      import javafx.scene.control.SelectionMode;
      import javafx.scene.control.TreeItem;
      import javafx.scene.control.TreeView;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class SelectionProblem extends Application {

          public void start(final Stage primaryStage) throws Exception {
              final VBox box = new VBox();
              final Scene scene = new Scene(box);

          private Node getTreeView() {
              final TreeItem<String> treeItem2 = new TreeItem<String>("Item 2");
              treeItem2.getChildren().addAll(new TreeItem<String>("Item 21"), new TreeItem<String>("Item 22"));

              final TreeItem<String> root1 = new TreeItem<String>("Root Node 1");
              root1.getChildren().addAll(new TreeItem<String>("Item 1"), treeItem2, new TreeItem<String>("Item 3"));

              final TreeItem<String> root2 = new TreeItem<String>("Root Node 2");

              final TreeItem<String> hiddenRoot = new TreeItem<String>("Hidden Root Node");

              final TreeView<String> treeView = new TreeView<String>(hiddenRoot);

              // listener which prints selected items
              treeView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<TreeItem<String>>() {
                  public void onChanged(final ListChangeListener.Change<? extends javafx.scene.control.TreeItem<String>> c) {
              return treeView;

          public static void main(final String[] args) {




            jgiles Jonathan Giles
            tkleszczyjfx Tomasz Kleszczynski (Inactive)
            0 Vote for this issue
            3 Start watching this issue