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

TreeView.getSelectionModel().getSelectedItem() can return null instead of item

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_10

      Description

      A DESCRIPTION OF THE PROBLEM :
      I have old application on jfx bundled with oracle jdk 1.8 working fine.

      i have upgraded it to the latest jfx14 but found the following issue:

      TreeView.getSelectionModel().getSelectedItem() returns null even though that
      TreeView.getSelectionModel().getSelectedItems().size() = 1

      the issue happens only when programmatically select a tree node using:
      *************************
      tree.getSelectionModel().clearSelection();
      tree.getSelectionModel().select(node);




      REGRESSION : Last worked in version 8u241

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the sample code

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -

      TreeView.getSelectionModel().getSelectedItem() to return the selected node object
      ACTUAL -

      TreeView.getSelectionModel().getSelectedItem() return null

      ---------- BEGIN SOURCE ----------
      import javafx.application.Application;
      import javafx.collections.ListChangeListener;
      import javafx.scene.Scene;
      import javafx.scene.control.TreeItem;
      import javafx.scene.control.TreeView;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;

      public class TreeViewExample extends Application
      {
          public static void main(String[] args)
          {
              launch(args);
          }

          @Override
          public void start(Stage primaryStage)
          {
              TreeItem<String> rootItem = new TreeItem<>("Tutorials");
              TreeItem<String> javaItem = new TreeItem<>("Java Tutorials");
              TreeItem<String> javaItem2 = new TreeItem<>("Java Language");
              TreeItem<String> javaItem3 = new TreeItem<>("Java Collections");
              TreeItem<String> javaItem4 = new TreeItem<>("Java Concurrency");
              javaItem.getChildren().add(javaItem2);
              javaItem.getChildren().add(javaItem3);
              javaItem.getChildren().add(javaItem4);
              rootItem.getChildren().add(javaItem);

              TreeView<String> treeView = new TreeView<>();
              treeView.setRoot(rootItem);

              treeView.setShowRoot(false);

              VBox vbox = new VBox(treeView);
              Scene scene = new Scene(vbox);
              primaryStage.setScene(scene);
              primaryStage.show();

              treeView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<TreeItem<String>>()
              {
                  @Override
                  public void onChanged(Change<? extends TreeItem<String>> change)
                  {
                      int i = treeView.getSelectionModel().getSelectedItems().size();
                      System.out.println(i); // i =1

                      TreeItem<String> node = treeView.getSelectionModel().getSelectedItem();
                      if (node == null)
                          System.out.println("node is null"); // In javafx-14 with jdk-14
                      else
                          System.out.println("node is not null"); // in javafx bundled with jdk-8
                  }
              });

              treeView.getSelectionModel().clearSelection();
              treeView.getSelectionModel().select(javaItem3);

              final int row = treeView.getRow(javaItem3);
              treeView.scrollTo(row);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      use the older javafx bundled with 1.8

      FREQUENCY : always


        Attachments

          Activity

            People

            • Assignee:
              aghaisas Ajit Ghaisas
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

              • Created:
                Updated: