Reputation: 57162
I've implemented drag and drop in my JTree and it works, but I find this to be a bit confusing. In the picture, I'm showing how the drop location between item4
and item5
can vary depending on how far down between the nodes you are. I'm guessing this is because the row boundaries for item4
and item5
meet in the middle between them, and depending on which side you are on, you are actually in a different row.
From a user perspective, I think this is not natural. I would think that if I dropped above a node, the drop would occur above it. If I dropped below the node, the drop would occur below it. Is there a way to configure that sort of behavior?
EDIT: Adding code to show how to get drop location
DropLocation dropLoc = support.getDropLocation();
Point dropPoint = dropLoc.getDropPoint();
tree.getTree().getPathForLocation(dropPoint.x, dropPoint.y);
Note that support
is a TransferSupport
object
EDIT 2: I seemed to have solved this problem by checking if the drop point is above or below the halfway point of the node. Then I can tell if the drop was above or below which node.
Upvotes: 4
Views: 546
Reputation: 1122
You have sort out of the answer, but since i have sort out the code, thinking still worth to post it.
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");
}
}
Upvotes: 2