VariableHeightLayoutCache.treeNodesChanged() skips updateYLocationsFrom()

XMLWordPrintable

    • Type: Bug
    • Resolution: Cannot Reproduce
    • Priority: P3
    • 7
    • Affects Version/s: 6, 7
    • Component/s: client-libs
    • generic, x86
    • generic, windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0"
      Java(TM) SE Runtime Environment (build 1.6.0-b105)
      Java HotSpot(TM) Client VM (build 1.6.0-b105, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      javax.swing.tree.VariableHeightLayoutCache skips updateYLocationsFrom() when evaluating children of an invisible root.

      VariableHeightLayoutCache contains the following code

      public void treeNodesChanged(TreeModelEvent e) {
          if(e != null) {
      . . .
      changedNode = getNodeForPath(e.getTreePath(), false, false);
      . . .
      if(!isFixedRowHeight()) {
      int aRow = changedNode.getRow();

      if(aRow != -1)
      this.updateYLocationsFrom(aRow);
      }
      . . .
          }
      }

      e.getTreePath() points to the parent of the nodes that were changed.

      If said parent is the root, and setRootVisible()==false, then changedNode will not be in the visibleNodes Vector, and thus aRow will be set to -1.

      This means that row height changes to top level nodes in a JTree will not change the Y-offsets of nodes that follow them. This is bad (tm).

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The same result as when s has the initial value "Two\nThree"
      ACTUAL -
      One
      Two
      Three
      Five
      Six
      (with Four occasionally displacing Three)
      Also, odd behaviour when selecting nodes.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------

      import java.awt.*;
      import javax.swing.*;
      import javax.swing.tree.*;

      public class TreeTest {

          static class ComponentCellRenderer extends JTextArea implements TreeCellRenderer {
              public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
                      boolean leaf, int row, boolean hasFocus) {
                  setText(value.toString());
                  if (selected) setForeground(selectionForeground);
                  else setForeground(textForeground);
                  if (selected) setBackground(selectionBackground);
                  else setBackground(textBackground);
                  return this;
              }
          }

          static String s = "Two-Three";
          static Color selectionForeground = UIManager.getColor("Tree.selectionForeground");
          static Color textForeground = UIManager.getColor("Tree.textForeground");
          static Color selectionBackground = UIManager.getColor("Tree.selectionBackground");
          static Color textBackground = UIManager.getColor("Tree.textBackground");

          public static void main(String[] args) {
              JFrame f = new JFrame();

              Object relay = new Object() {public String toString() {return s;}};
              JTree tree = new JTree(new Object[]{"One", relay, "Four", "Five", "Six"});
              tree.setCellRenderer(new ComponentCellRenderer());
              f.add(tree);
              f.pack();
              f.setVisible(true);
              s = "Two\nThree";
              DefaultTreeModel model = (DefaultTreeModel) tree.getModel();
              DefaultMutableTreeNode root = (DefaultMutableTreeNode) model.getRoot();
              model.nodeChanged(root.getChildAt(1));
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Manually calling treeState.invalidatePathBounds(tree.getPathForRow(0)); when appropriate. Note that it has to go rather late in the sequence, because other wise the height change won't have happened yet.

            Assignee:
            Alexander Potochkin (Inactive)
            Reporter:
            Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: