File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes problem with moving a JWindow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with moving a JWindow" Watch "problem with moving a JWindow" New topic

problem with moving a JWindow

kalle svensson

Joined: Oct 05, 2009
Posts: 13
I wonder how to get a JWindow to move across the screen in location after the user's mouse pointer.
When i use the method setLocation((MouseEvent).getX(), (MouseEvent).getY()) in an mousemotion adapter attched to the JWindow a
lot of flicker occurs on the screen. In the code above a JWindow appears and when the user clicks on the yes button then the text "good luck"
displays before the window is closed after a small delay.

The code below is written just for this post. How can i move the window without generating the flicker, what is the general solution for this problem?
I will be very much greatful for any help on this subject.
// kalle

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2454
The main problem is that the MouseEvents are relative to the component and so the location never changes by more than a pixel, so you keep set the window location back to where it was even as the mouse is continually moving acros the screen.

Generally, moving a component is handled by dragging the component which mean using the mouseDragged() event. A general solution can be found in Moving Windows. This solution converts the mouse events to make the location relative to the screen.

For an older solution that doesn't convert mouse points to the screen, you need to keep track of the previous mouse event so you can determine the change in location:

I agree. Here's the link:
subject: problem with moving a JWindow
It's not a secret anymore!