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

TreeView selection incorrectly changes after deleting an unselected row

XMLWordPrintable

    • generic
    • generic

        FULL PRODUCT VERSION :
        java version "1.8.0_144"
        Java(TM) SE Runtime Environment (build 1.8.0_144-b01)
        Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

        ADDITIONAL OS VERSION INFORMATION :
        Microsoft Windows [Version 10.0.15063]
        Microsoft Windows [Version 6.1.7601]

        A DESCRIPTION OF THE PROBLEM :
        When a row is removed from a TreeView hierarchy, the selection can incorrectly move. In my case, this seems to happen when the removed item is a sibling of the selected item's ancestor, and the removed item is in a lower row than the selected item. Debugging the TreeView class suggests that in the EventHandler treeItemListener, variable startRow is not being calculated properly.

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        See test case

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        When we delete a row that is lower than the currently selected row, neither the selection nor the selection index should change. Test case below should pass.
        ACTUAL -
        Both selection and selection index change. Test case below fails both assertions.

        ERROR MESSAGES/STACK TRACES THAT OCCUR :
        No error

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        @Test
          public void testRemoveTreeItemShiftSelection(){
            TreeItem<String> a, b, a1, a2, a3;
            TreeItem<String> root = new TreeItem<>("root");
            root.getChildren().addAll(
                a = new TreeItem<>("a"),
                b = new TreeItem<>("b")
            );
            root.setExpanded(true);

            a.getChildren().addAll(
                a1 = new TreeItem<>("a1"),
                a2 = new TreeItem<>("a2"),
                a3 = new TreeItem<>("a3")
            );
            a.setExpanded(true);

            TreeView<String> stringTreeView = new TreeView<>(root);
            stringTreeView.setShowRoot(false);
            SelectionModel sm = stringTreeView.getSelectionModel();

            //rows:
            //a
            //a1
            //a2
            //a3
            //b

            sm.clearAndSelect(3); //select a3
            assertEquals(a3, sm.getSelectedItem()); //verify
            root.getChildren().remove(b); //remove b
            //a3 should remain selected
            assertEquals(3, sm.getSelectedIndex());
            assertEquals(a3, sm.getSelectedItem());
          }
        ---------- END SOURCE ----------

        CUSTOMER SUBMITTED WORKAROUND :
        Workaround is to manually sync the selected row to the selected item in the business logic.

              jpereda Jose Pereda
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Created:
                Updated:
                Resolved: