• Post Reply Bookmark Topic Watch Topic
  • New Topic

Drag and Drop Problem  RSS feed

 
Alex Qiu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,I am trying to perform a drag-and-drop feature on the JTree .
Now encounter a problem , it is :
My custom tree is in a scrollpane .
So during drag and drop ,
in the DropTargetListener , dragOver Event
public void dragOver(DropTargetDragEvent dtde){
// this method get the point in obsolute coordinate .
Point p = dtde.getLocation();
// Then want to get the current TreeNode under the point of the mouse
// in this method , the (x, y) seems to need to use the relative coordinate .
// now it should provide the point base on the scrollpane .
TreePath path = TreeView.this.getPathForLocation(p.x, p.y);
// Then the path is null , even the mouse is over a treenode .
// coz the TreeView is added in a scrollPane and
// the scrollPane is added on another panel .
...
}
Any one help me to solve this problem ? Or tell me how to deal with it ...
Any help is appreciated .
Thank you in advance .
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What component is listening for the drop? The tree, scrollpane, or panel? Coordinate space may be different in all of them. If another component is listening to the drag and drop, it gets points in its coordinates. You would have to convert the point into the coordinates of the tree if you want to find out where on the tree the event happened.

Also, try printing out the coordinates of the event and the coordinates of the cells. This may give you some insight as to what may be going wrong. (getRowBounds(int) will get the bounds of a specific row, from which you can get the location. ) There is also a getClosestPathForLocation(x,y) method that may help...
 
Alex Qiu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great thanks to you , Nathan .
I am confused in it for a whole day .
Mine custom tree is in a scrollpane , the scrollpane is in a panel(here call myPanel) which I pack it as a Java Bean .
Then I set add myPanel in the Frame.getContentPane() .
I do print the point in console when dragOver .
It shows out to be the point based on the frame(or maybe the contentPane) .
And this DND feature runs well when I put the treeview on the top left of the frame .
(overlap the 2 coordinates)
So I think the method of the tree getPathForLocation(x, y); here I need to provide its relative coordinate . Maybe related to its parent . The JViewport .
But the getLocation() of JViewport returns (0,0)
which its location maybe also related to its parent JScrollPane ...
As I have to pack the whole thing into a java bean . How can I accomplish this ?
Have you any idea on it ?
Thanks again for your patient .
 
Alex Qiu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excuse me , the treeView is listening for the job .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!