For a direct (as best as I can) answer to the question asked, no, you can not set the global cursor using Java. This is largely operating system dependent, but I would like to think that, for security reasons, setting the global cursor is blocked on most, if not all, secure operating systems.
However, it's also important to touch on a method I thought would work, but doesn't appear to work on my system, being a 64-bit Windows 10 Pro.
Inspired from this answer, you can make a fully transparent window that passes events through to the windows behind them. (see code sample below)
Now, the definition of the Window.setOpacity
states this is platform-dependent behavior, but it's specifically about how MouseEvents are handled, not how the mouse cursor is handled. However, according to the Windows Documentation, setting the cursor is based on a specific event, so we'd need control which events get passed through. So, this becomes more of a lower-level (most likely C/C++) question rather than a Java question.
Here's the code sample I made to test it:
JFrame frame = new JFrame();
frame.setSize(1920, 1080); // set the size
frame.setLocationRelativeTo(null); // center the window
frame.setUndecorated(true); // make it so the frame is a basic rectangle, no topbar or outline.
frame.setAlwaysOnTop(true); // make it so the frame is on top
frame.setOpacity(0.0f); // make the frame transparent.
frame.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
frame.setVisible(true);
If you set the parameter in frame.setOpacity(0.0f);
from 0.0f
to something between 0 and 1, you'll see that the cursor actually changes.
If you want to venture into the land of natives and using hooks (maybe you don't have to go that deeply), maybe start here. Whenever I program in C/C++ (or any other lower level language), I tend to be self-contained with my code, never using the WinAPI and relying on libraries like SDL, so I'm not well-versed on how these system-level functions work.
TL;DR, in standard Java, you cannot. Lower-level options can work.
I guess you could also just make your own operating system, even contain it within Java so you don't have to install a whole new partition onto your hard drive.