JTree Fall Index Between Nodes

I implemented drag and drop in my JTree and it works, but I find this a bit confusing. In the picture, I show how the drag location between item4 and item5 can vary depending on how far between the nodes you are. I guess this is because the line borders for item4 and item5 meet in the middle between them, and depending on which side you are on, you are actually on another line.

From the user's point of view, I think this is not natural. I would think that if I go down above the node, the fall will happen over it. If I went below the node, a drop will occur under it. Is there a way to customize this behavior?

enter image description here

EDIT: adding code to show how to get a place to delete

  DropLocation dropLoc = support.getDropLocation(); Point dropPoint = dropLoc.getDropPoint(); tree.getTree().getPathForLocation(dropPoint.x, dropPoint.y); 

Please note that support is a TransferSupport object

EDIT 2: I seem to have solved this problem by checking if the drop point is above or below half of the node point. Then I can determine if the drop was higher or lower than the node.

+4
source share
1 answer

You have a choice of answer, but since I understand the code, I think it's worth publishing it anyway.

  public void dragOver(DropTargetDragEvent dtde) { Point dropPoint = dtde.getLocation(); TreePath path = tree.getPathForLocation(dropPoint.x, dropPoint.y); Rectangle pathBounds = tree.getPathBounds(path); Rectangle topHalf = new Rectangle(pathBounds.x, pathBounds.y, pathBounds.width, pathBounds.height / 2); if (topHalf.contains(dropPoint)) { System.out.println("top half"); } else { System.out.println("bottom half"); } } 
+2
source

Source: https://habr.com/ru/post/1402723/


All Articles