DYL
DYL

Reputation: 367

How to change cursor icon in Java?

I would like to change the cursor icon to my customized 32x32 image when a Java application is executing. I looked and searched, those I found are just setting cursor on a JComponent. But I want the cursor changed to my specified icon wherever it goes moving, browsing, and click, as long as the Java application is still running, or you can say program runtime.

Thanks alot.

Upvotes: 34

Views: 77476

Answers (6)

Noah the Epic Guy
Noah the Epic Guy

Reputation: 31

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.

Upvotes: 1

Abhishek Singh
Abhishek Singh

Reputation: 21

public void mouseEntered(MouseEvent e)
{
// set cursor for frame and its component
//  this is the current frame you are using .
//  You can change the this keyword with your frame name .

java.awt.Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("/images/mousepoint.jpg");
Cursor a = toolkit.createCustomCursor(image , new Point(this.getX(),this.getY()), "");
this.setCursor (a);
}

Upvotes: 2

Mohamed Saligh
Mohamed Saligh

Reputation: 12339

Standard cursor image:

setCursor(Cursor.getDefaultCursor());

User defined Image:

Toolkit toolkit = Toolkit.getDefaultToolkit();
Image image = toolkit.getImage("icons/handwriting.gif");
Cursor c = toolkit.createCustomCursor(image , new Point(mainPane.getX(), 
           mainPane.getY()), "img");
mainPane.setCursor (c);

You can download a zip containing sample source: HERE

Upvotes: 51

stacker
stacker

Reputation: 68942

Call Component.setCursor. The class Cursor as a few predefined cursors.

A custom cursor image can be created:

setCursor(Toolkit.getDefaultToolkit().createCustomCursor(
new ImageIcon("custom.png").getImage(),
new Point(0,0),"custom cursor"));

Upvotes: 12

Tony
Tony

Reputation: 414

Why don't you have a class MyFrame which exteds JFrame. All it does is call the JFrame constructor and sets the cursor to your desired cursor. In my application we have a touch screen with no cursor so this is how I intend to implement it.

Upvotes: 0

camickr
camickr

Reputation: 324098

Try settin the cursor on the rootPane.

frame.getRootPane().setCursor(...);

Upvotes: 5

Related Questions