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

The position of the rectangle during dragging JTreeNode is not correct

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Cannot Reproduce
    • Icon: P3 P3
    • 9
    • 7u21
    • client-libs

      FULL PRODUCT VERSION :
      java version " 1.7.0_21 "
      Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
      Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Mac OS X 10.7.5

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Aqua LnF

      A DESCRIPTION OF THE PROBLEM :
      When we drag a node of JTree, a rectangle is shown. But the position of the rectangle is not correct. The rectangle is drawn upper location than the correct location. We don't have this problem on Windows. We don't have this problem with Java6 on MacOSX.

      REGRESSION. Last worked in version 6u45

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Run the program in this report
      2. Drag a node in the tree

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The rectangle should be shown around the mouse pointer.
      ACTUAL -
      The rectangle is shown upper location than the appropriate location.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.datatransfer.*;
      import java.awt.dnd.*;
      import java.io.*;
      import javax.swing.*;
      import javax.swing.tree.*;

      public class DraggableTree extends JPanel {
          public DraggableTree() {
              super(new BorderLayout());
              DefaultTreeModel model = makeModel();
              DnDTree tree = new DnDTree();
              tree.setModel(model);
              for(int i=0;i<tree.getRowCount();i++) {
                  tree.expandRow(i);
              }
              add(new JScrollPane(tree));
              setPreferredSize(new Dimension(320, 240));
          }
          private DefaultTreeModel makeModel() {
              DefaultMutableTreeNode root = new DefaultMutableTreeNode( " Root " );
              DefaultMutableTreeNode set1 = new DefaultMutableTreeNode( " Set 001 " );
              DefaultMutableTreeNode set2 = new DefaultMutableTreeNode( " Set 002 " );
              DefaultMutableTreeNode set3 = new DefaultMutableTreeNode( " Set 003 " );
              set1.add(new DefaultMutableTreeNode( " 111111111 " ));
              set1.add(new DefaultMutableTreeNode( " 22222222222 " ));
              set1.add(new DefaultMutableTreeNode( " 33333 " ));
              set2.add(new DefaultMutableTreeNode( " asdfasdfas " ));
              set2.add(new DefaultMutableTreeNode( " asdf " ));
              set3.add(new DefaultMutableTreeNode( " asdfasdfasdf " ));
              set3.add(new DefaultMutableTreeNode( " 5555555555 " ));
              set3.add(new DefaultMutableTreeNode( " 66666666666666 " ));
              root.add(set1);
              root.add(set2);
              set2.add(set3);
              return new DefaultTreeModel(root);
          }

          public static void main(String[] args) {
              EventQueue.invokeLater(new Runnable() {
                  @Override public void run() {
                      createAndShowGUI();
                  }
              });
          }
          public static void createAndShowGUI() {
              try{
                  UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              }catch(Exception e) {
                  e.printStackTrace();
              }
              JFrame frame = new JFrame( " DnDTree " );
              frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              frame.getContentPane().add(new DraggableTree());
              frame.pack();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
          }
      }

      class DnDTree extends JTree implements DragSourceListener, DragGestureListener {
          private static final String NAME = " TREE-TEST " ;
          private static final DataFlavor localObjectFlavor = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType, NAME);
          private static final DataFlavor[] supportedFlavors = { localObjectFlavor };
          private TreeNode dropTargetNode = null;
          private TreeNode draggedNode = null;

          public DnDTree() {
              super();
              setModel(new DefaultTreeModel(new DefaultMutableTreeNode( " default " )));
              new DragSource().createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_MOVE, this);
          }

          @Override public void dragGestureRecognized(DragGestureEvent dge) {
              Point pt = dge.getDragOrigin();
              TreePath path = getPathForLocation(pt.x, pt.y);
              if(path==null || path.getParentPath()==null) {
                  return;
              }
              draggedNode = (TreeNode) path.getLastPathComponent();
              Transferable trans = new RJLTransferable(draggedNode);
              new DragSource().startDrag(dge, Cursor.getDefaultCursor(), trans, this);
          }

          @Override public void dragDropEnd(DragSourceDropEvent dsde) {
              dropTargetNode = null;
              draggedNode = null;
              repaint();
          }
          @Override public void dragEnter(DragSourceDragEvent dsde) {
              dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
          }
          @Override public void dragExit(DragSourceEvent dse) {
              dse.getDragSourceContext().setCursor(DragSource.DefaultMoveNoDrop);
          }
          @Override public void dragOver(DragSourceDragEvent dsde) {}
          @Override public void dropActionChanged(DragSourceDragEvent dsde) {}

          static class RJLTransferable implements Transferable {
              Object object;
              public RJLTransferable(Object o) {
                  object = o;
              }
              @Override public Object getTransferData(DataFlavor df) throws UnsupportedFlavorException, IOException {
                  if(isDataFlavorSupported(df)) {
                      return object;
                  }else{
                      throw new UnsupportedFlavorException(df);
                  }
              }
              @Override public boolean isDataFlavorSupported(DataFlavor df) {
                  return df.getHumanPresentableName().equals(NAME);
              }
              @Override public DataFlavor[] getTransferDataFlavors() {
                  return supportedFlavors;
              }
          }
      }

      ---------- END SOURCE ----------

            pchelko Petr Pchelko (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: