• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with drag and drop location

 
David Garratt
Ranch Hand
Posts: 213
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to master drag and drop by putting together various code snippets from various google sources. It's almost working as intended apart from an annoying problem locating the dropped item on a target panel. The code around lines 80-90 deals with the placement of the dropped panel object.

The issue seems to be that unless the user drags the source panel by clicking on the 0,0 top left edge the co-ordinates where the item is dropped will be offsite by the distance between the cursor when it is clicked and the 0,0 position within the source object. Somehow I need to determine how far away the mouse pointer is from 0,0 of my source panel and use those figures to adjust the placement of my object when it's dropped.

I've not explained that very well.

Sorry.


dg1.jpg
[Thumbnail for dg1.jpg]
dg2.jpg
[Thumbnail for dg2.jpg]
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Garratt wrote:Somehow I need to determine how far away the mouse pointer is from 0,0 of my source panel


MouseEvent#getX() / getY()
 
David Garratt
Ranch Hand
Posts: 213
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok - I have put some debug code in my mouse event handler and it returns plausible values. Now I wonder - how to I communicate these "offset" values back to the code which does the "dropping"

At the risk of pushing my luck too far please have a look at the following.

1) This is the code which I use to represent each toolbar "icon" - panel to the right of my form. Line 31 assigns the mouse listener.



2) This is the mouse listener code referenced above.



3) Finally the code which handles the "drop" (same as initial post).



I suppose I could have some public static variables for x and y somewhere - but that seems ugly.

Any further guidance would be most appreciated.

Thanks

Dave
 
Darryl Burke
Bartender
Posts: 5154
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Certainly not static, and I don't see why you would want any such variables to be public. Play with this and you may be able to adapt it to your more complex task.
 
David Garratt
Ranch Hand
Posts: 213
Eclipse IDE Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I seems like I'm getting too old to learn all this new stuff these days - or at best I have to forget 2 things in order to make room for 1 new thing.

Your example has a mouse listener attached to the form/panel that the object already belongs to. In my code I have a mouse listener which belongs to the object being dragged - which initially is not part of the same object hierarchy.

Check the screen dump below.

So as I understand it the visibility/scope of the mouse event handler in my drop code is not accessible to me.

Does this mean I need another mouse event handler on the drop panel as well ?
dg4.jpg
[Thumbnail for dg4.jpg]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!