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

[TreeTableView] selected item and focused item are incorrectly updated on collapsing

XMLWordPrintable

      Run the code:

          TreeTableView<Data> treeTableView;

          public static void main(String[] args) {
              launch(args);
          }

          @Override
          public void start(Stage stage) throws Exception {
              treeTableView = new TreeTableView<Data>();
              treeTableView.setMaxHeight(100);
              treeTableView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);

              treeTableView.setRoot(new TreeItem<Data>(new Data("Root")));
              for (int i = 0; i < 3; i++) {
                  treeTableView.getRoot().getChildren().add(new TreeItem<Data>(new Data("item" + String.valueOf(i))));
                  addItems(treeTableView.getRoot().getChildren().get(i),3);
              }
              treeTableView.getRoot().setExpanded(true);

              treeTableView.getColumns().add(new TreeTableColumn<Data, String>("Data"));
              ((TreeTableColumn<Data, String>) treeTableView.getColumns().get(0)).setCellValueFactory(new Callback<CellDataFeatures<Data, String>, ObservableValue<String>>() {
                  @Override
                  public ObservableValue<String> call(CellDataFeatures<Data, String> p) {
                      return p.getValue().getValue().property;
                  }
              });

              treeTableView.getSelectionModel().selectedIndexProperty().addListener(new ChangeListener() {
                  @Override
                  public void changed(ObservableValue ov, Object t, Object t1) {
                      System.out.println("selectedIndexProperty : " + t1);
                  }
              });

              treeTableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TreeItem<Data>>() {
                  @Override
                  public void changed(ObservableValue<? extends TreeItem<Data>> ov, TreeItem<Data> t, TreeItem<Data> t1) {
                      System.out.println("selectedItemProperty : " + t1);
                  }
              });

              treeTableView.getSelectionModel().getSelectedItems().addListener(new ListChangeListener<TreeItem<Data>>() {
                  @Override
                  public void onChanged(Change<? extends TreeItem<Data>> change) {
                      System.out.println("SelectedItems : " + treeTableView.getSelectionModel().getSelectedItems());
                  }
              });

              treeTableView.getFocusModel().focusedIndexProperty().addListener(new ChangeListener<Number>() {
                  @Override
                  public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                      System.out.println("focusedIndexProperty : " + t1);
                  }
              });

              treeTableView.getFocusModel().focusedItemProperty().addListener(new ChangeListener<TreeItem<Data>>() {
                  @Override
                  public void changed(ObservableValue<? extends TreeItem<Data>> ov, TreeItem<Data> t, TreeItem<Data> t1) {
                      System.out.println("focusedItemProperty : " + t1);
                  }
              });

              VBox vb = new VBox();
              vb.getChildren().addAll(treeTableView);

              Scene scene = new Scene(vb, 300, 300);
              stage.setScene(scene);
              stage.show();
          }

          private void addItems(TreeItem<Data> item, int amount) {
              for (int i = 0; i < amount; i++) {
                  item.getChildren().add(new TreeItem<Data>(new Data(item.getValue().property.get() + "-" + String.valueOf(i))));
              }
          }

          class Data {

              public StringProperty property;

              public Data(String data) {
                  property = new SimpleStringProperty(data);
              }

              @Override
              public String toString() {
                  return property.get();
              }
          }

      expand all items.
      select some item.
      press ctrl+a.

      collapse some item in the middle (item 1 for instance). focus and selected item will be moved to that position. I expect them to stay, where they were before collapsing.

      Sample output:
      SelectedItems : [TreeItem [ value: item0-1 ]]
      selectedItemProperty : TreeItem [ value: item0-1 ]
      selectedIndexProperty : 3
      focusedItemProperty : TreeItem [ value: item0-1 ]
      focusedIndexProperty : 3
      SelectedItems : []
      SelectedItems : [TreeItem [ value: item0-1 ]]
      SelectedItems : []
      SelectedItems : [TreeItem [ value: Root ], TreeItem [ value: item0 ], TreeItem [ value: item0-0 ], TreeItem [ value: item0-1 ], TreeItem [ value: item0-2 ], TreeItem [ value: item1 ], TreeItem [ value: item1-0 ], TreeItem [ value: item1-1 ], TreeItem [ value: item1-2 ], TreeItem [ value: item2 ], TreeItem [ value: item2-0 ], TreeItem [ value: item2-1 ], TreeItem [ value: item2-2 ]]
      selectedItemProperty : TreeItem [ value: item2-2 ]
      selectedIndexProperty : 12
      focusedItemProperty : TreeItem [ value: item2-2 ]
      focusedIndexProperty : 12
      selectedItemProperty : TreeItem [ value: item1 ] // here I collapsed the item.
      selectedIndexProperty : 5
      focusedItemProperty : TreeItem [ value: item1 ]
      focusedIndexProperty : 5

      BTW, selected items array was not updated.

      Affected tests:
      ControlsAutomatedTestSuite/javafx/scene/control/test/treetable/TreeTableAsTreeTest/selectionCorrectnessOnCollapsingCtrlATest

            jgiles Jonathan Giles
            akirov Alexander Kirov (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: