How to change cursor icon in Java?
Asked Answered
R

6

34

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.

Ruelas answered 25/11, 2010 at 7:27 Comment(0)
R
51

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

Ritch answered 25/11, 2010 at 7:34 Comment(5)
thx anyway, but again, not setting cursor just on a component, pretty sure "mainpane" is a component such as panel. But i want it to be changed wherever it goes, not limited to the frame or panel. ThxRuelas
you can simply use frame instead of panel. if you extending your class using JFrame, you simply use "this". hope you are clear now...Ritch
but even using frame, the customized cursor is only limited within the frame scope right?? if you move the cursor out side of the frame, it restores to the default system cursor. I dont want this though...Ruelas
and the second argument of createCustomCursor is for hotspot, which is within the preferrable size of the default cursor. getX() and getY() wont do it since they are plain coords of the frame whatsoeverRuelas
I don't know whether possible or not. Since, you need to override your operating system pointer icon.Ritch
G
12

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"));
Goiter answered 25/11, 2010 at 7:33 Comment(3)
but that is for a frame, like i said, i want to extend to the system too during application runtime.Ruelas
Custom cursor link is brokenStace
I couldn't new ImageIcon() without using getClass().getResource("custom.png")Spinode
G
5

Try settin the cursor on the rootPane.

frame.getRootPane().setCursor(...);
Grecoroman answered 25/11, 2010 at 15:25 Comment(2)
thx though, still only within the pane or frame. I do not why it can be done in C# or C++, but no clue what to do in Java.Ruelas
And that is the way it should work. Java only has control of the frame, not the entire desktop. I would not use your application if you tried to control the icon when it was on another application.Grecoroman
C
2
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);
}
Corfu answered 22/3, 2015 at 9:0 Comment(0)
T
1

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.

Tezel answered 26/12, 2022 at 21:53 Comment(0)
F
0

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.

Firman answered 17/8, 2012 at 6:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.