• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help dragging a jpanel please...

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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) {
mypanel.setLocation(mypanel.getX()+e.getX(),
mypanel.getY()+e.getY());
}
any help appreciated, thanks...
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
brenda stewart
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After 20 years , a new reply to Valentin Creddaz's code worked like a charm.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those who are interested in what Valentin Crettaz is doing now, see an interview with him, including kudos for the Ranch.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HS: please don't edit posts after a reply. I am refusing the edit.
 
Hope Spartan
Ranch Hand
Posts: 56
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:HS: please don't edit posts after a reply. I am refusing the edit.



Hey, it is not correct : I first edited my reply before Paul's reply ! So when I edited my reply, no other reply was there. Timing is important, if you look at the timings you will see that I am right.

Also, I edited my post because :
1. Mr.Valentin's code is better than mine since no focus lost occurs because I used mouseMoved() not mouseDragged . So mine code can misguide the readers, Mr. Valentin's code idea is the best for dragging.

I edited my post after Paul's reply since, I wrongly spelled Mr. Valentin's surname, it should be Crettaz, not Creddaz.  Writing a correct name-surname is very important in my country. It is my mistake. I am sorry from Mr. Valentin here.

I hoped you accept these edits to allow poster to correct him/her mistakes. I think I can edit such mistakes as poster, can't I ?
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My apologies; I was obviously mistaken. I have just tried and failed to return the post to its state on Monday.
No, repeated editing is a bad idea; it causes other readers to become confused about what the question really is.
 
reply
    Bookmark Topic Watch Topic
  • New Topic