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

Editability problem in DefaultTreeCellEditor.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.2
    • client-libs

      FULL PRODUCT VERSION :
      java version "1.4.2_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
      Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

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

      (This bug is OS independent.)

      A DESCRIPTION OF THE PROBLEM :
      The problem is that if one clicks on a tree cell and the mouse event's x location is smaller than the last edited cell's real editor's starting x position, then the cell being clicked becomes non-editable to the user. (IsCellEditable() still returns true, but the editor component is not installed.)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
       * Try the following example. After expanding the "colors" tree node, do the
       * following:
       *
       * <UL>
       * <LI> Select the "JTree" node and click on "J" in "JTree" again, "JTree" becomes editable;
       * <LI> Select the "colors" and click on "c" in "colors" again, "colors" becomes editable;
       * <LI> Select the "blue" and click on "b" in "blue" again, "colors" becomes editable;
       * <LI> Select the "colors" and click on "c" in "colors" again, "colors" is NOT editable;
       * <LI> Select the "JTree" and click on "J" in "JTree" again, "JTree" is NOT editable;
       * </UL>
       *
       * <PRE>
       * public class TreeTest extends JFrame {
       *
       * public TreeTest() {
       * super();
       *
       * JTree tree = new JTree();
       * tree.setEditable(true);
       *
       * getContentPane().add(tree, BorderLayout.CENTER);
       * }
       *
       * public static void main(String[] args) {
       * TreeTest TreeTest = new TreeTest();
       * TreeTest.setBounds(200, 200, 200, 200);
       * TreeTest.show();
       * }
       * }
       * </PRE>

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      See "Steps to Reproduce".
      ACTUAL -
      See "Steps to Reproduce".

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.BorderLayout;
      import javax.swing.JFrame;
      import javax.swing.JTree;

      public class TreeTest extends JFrame{

          public TreeTest() {
              super();
              
              JTree tree = new JTree();
              tree.setEditable(true);
              
              getContentPane().add(tree, BorderLayout.CENTER);
          }

          public static void main(String[] args) {
              TreeTest TreeTest = new TreeTest();
              TreeTest.setBounds(100, 100, 200, 200);
              TreeTest.show();
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Replace the default tree cell editor by the following:

        tree.setCellEditor(new FixedDefaultTreeCellEditor(tree,
                      (DefaultTreeCellRenderer) tree.getCellRenderer()));


      public class FixedDefaultTreeCellEditor extends DefaultTreeCellEditor {
          
          private int lastRowForEditChecking = -1;
          
          public FixedDefaultTreeCellEditor(JTree tree, DefaultTreeCellRenderer renderer) {
              super(tree, renderer);
          }
          
          public FixedDefaultTreeCellEditor(JTree tree,
                  DefaultTreeCellRenderer renderer, TreeCellEditor editor) {
              super(tree, renderer, editor);
          }
          
          public boolean isCellEditable(EventObject event) {
              if (event != null
                      && event.getSource() instanceof JTree
                      && event instanceof MouseEvent) {
                  lastRowForEditChecking = tree.getRowForLocation(
                          ((MouseEvent)event).getX(),
                          ((MouseEvent)event).getY());
              }
              
              return super.isCellEditable(event);
          }
          
          protected boolean inHitRegion(int x, int y) {
              if (lastRowForEditChecking != -1 && tree != null) {
                  Rectangle bounds = tree.getRowBounds(lastRowForEditChecking);
                  
                  if (bounds != null && x <= (bounds.x + offset) &&
                          offset < (bounds.width - 5)) {
                      return false;
                  }
              }
              return true;
          }
      }

            svioletsunw Scott Violet (Inactive)
            rlewis Roger Lewis (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: