This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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: