"Capturing" the mouse in a Frame or JPanel

When the user puts the mouse cursor in a certain frame, I would like to make the pointer disappear and capture the mouse’s movement until the button is released. However, I’d ALSO like the mouse cursor to remain where it was when the user originally started the drag.

I understand how to use MouseListener, MouseMotionListener, and MouseWheelListener, and I’m also pretty sure I can make the cursor disappear. The tricky part seems to be keeping the mouse cursor from moving. How can I do that? Is there some way I can move the cursor to an arbitrary point? It seems like there is probably an interface to do that, but I can’t find it…

Thanks.

Maybe “java.awt.Robot” helps (set mouse position etc.)?

Making the cursor invisible? Mhh, I know you could set a transparent mouse cursor:


int[] pixels = new int[16 * 16];
Image image = Toolkit.getDefaultToolkit().createImage(
        new MemoryImageSource(16, 16, pixels, 0, 16));
Cursor transparentCursor =
        Toolkit.getDefaultToolkit().createCustomCursor
            (image, new Point(0, 0), "invisiblecursor");

I am not sure if there is a better solution?

Yep:

java.awt.Robot

I never would have thought to look there … thanks all.