File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes help dragging a jpanel please... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help dragging a jpanel please..." Watch "help dragging a jpanel please..." New topic

help dragging a jpanel please...

brenda stewart

Joined: Mar 29, 2002
Posts: 2
i finally got my jpanel to be draggable. my problem is when i click inside the panel to drag it, the panel's x,y start point jumps to my cursor .... i can only drag the panel from the upper left corner. (if i click in the middle of the panel, the entire panel jumps over to where my cursor is on its x,y start point...then i can drag it) how
can i drag the panel from wherever i click on it, instead of it jumping like that?

here is the code i'm using in my mouse motion listener to drag it...
void mypanel_mouseDragged(MouseEvent e) {
any help appreciated, thanks...
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Simply, you need to have a reference point, like the point where the mouse has been pressed the first time. Then on every mousevent, you can compute the deltas between the actual mouse location and the original one and move the panel's origin by that delta. Just after that move the reference point to the current one. In terms of code:

Of course this code is not optimized at all, improvements are possible too. But you get the idea...
[ March 29, 2002: Message edited by: Valentin Crettaz ]

[Blog] [Blogroll] [My Reviews] My Linked In
brenda stewart

Joined: Mar 29, 2002
Posts: 2
thank you very much for the info... i can now drag the panel from anywhere i click on it but now it redraws very very slow. the mouse cursor starts out on the jpanel when i drag it but when i move it the cursor moves 2 inches to every 1 inch the jpanel moves. it also flickers really bad. i do have the panel double buffered.
any ideas on how to make it smooth when it moves?
thanks again...
I agree. Here's the link:
subject: help dragging a jpanel please...
It's not a secret anymore!