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 someho开发者_开发百科w 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.
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.)
Which part of your 'timeline' is the actual component receiving the mouseDragged() event? Normally on receiving this event you would move the component to match the dragging action - that way, the component should always be under the mouse and always receive the mouseDragged() event until you let go of it. Are you moving this component as you process the events? A small button-like rectangle used as a slider would make an excellent component to use.
(If you are trying to treat your whole timeline as the component and are therefore not moving it to match, what do you want to happen if the user drags the mouse while outside?)
Although Windows (and possibly others) support capturing the mouse, Java does not appear to. You might be able to provide a custom event queue something like this:
Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener()
{
public void eventDispatched(AWTEvent e)
{
// Look for mouse messages and handle them...
}
}
, AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK);
JComponent.setAutoScrolls(...);
精彩评论