Search code examples
javamousemouseeventmouseout

Lock event on mouse exit java


I wrote an application where you can drag left and right some timeline. When i do dragging i know that this is a mouseDragged event. Since i move mouse cursor outside component it stops dragging, but i don't want that.

So i want to somehow lock mouseDragged event on that component, but if i move cursor outside component, i don't get mouseDragged event anymore.

How can i solve that? My colleague tell me that there exists some function like captureMouse.


Solution

  • I don't think it's such a good idea to capture the mouse. I also don't think it's necessary for what you want.

    In my experience, mouseDragged() events work fine even when you move the mouse outside of the component area: it will continue to report the X and Y positions inside the component that are closest to the mouse's location outside of the component, but it won't terminate the mouse drag as if you had released the left button.

    If the behaviour of your application is different, maybe you've got a different bug in your mouse handling?

    For example, try out the applet half way down this page: you'll see that you can start dragging the rectangle and move your mouse outside of the applet area, and it will continue to work: http://www.dgp.toronto.edu/~mjmcguff/learn/java/04-mouseInput/

    (I don't know the author of the code above; it's just one of the first I found with a search of java applet mousedragged.)