How to Hide Cursor in a Swing Application

How to hide cursor in a Swing application?

It appears that the Cursor class does not have a "blank" cursor to begin with, so one could define a new "blank" cursor using the Toolkit.createCustomCursor method.

Here's one way I've tried which seems to work:

// Transparent 16 x 16 pixel cursor image.
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

// Create a new blank cursor.
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
cursorImg, new Point(0, 0), "blank cursor");

// Set the blank cursor to the JFrame.
mainJFrame.getContentPane().setCursor(blankCursor);

Edit

Regarding the comment about everything inside the JFrame ending up without a cursor, it seems that the Components which are contained in the JFrame will end up inheriting the cursor of the container (the JFrame), so if it is a requirement to have a certain Component have the cursor appear, one would have to manually set the desired cursor.

For example, if there is a JPanel contained in the JFrame, then one could set the cursor of that JPanel to the system's default using the Cursor.getDefaultCursor method:

JPanel p = ...
// Sets the JPanel's cursor to the system default.
p.setCursor(Cursor.getDefaultCursor());

Hide Java cursor at any place on the screen

You can use a MouseMotionListener and override its mouseMoved method to check where the mouse is (in the components) and set the cursor type accordingly:

public class Test extends JFrame {

Test() {
final int x1 = 100, y1 = 100, x2 = 300, y2 = 300;

BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(cursorImg, new Point(0, 0), "blank cursor");
Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);

addMouseMotionListener(new MouseAdapter() {

@Override
public void mouseMoved(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (x > x1 && x < x2 && y > y1 && y < y2)
setCursor(blankCursor);
else
setCursor(defaultCursor);
}
});

setSize(new Dimension(400, 400));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new Test());
}
}

Credit to this answer for the trick of a blank cursor.

You cannot set the cursor outside of a Java window. You can go with your idea of an invisible, fullscreen, headless frame, but this is pretty dodgy and will also intercept all mouse events. Implementation will be the same.

Hide mouse cursor on whole screen in Java

One way is to use transperent GIF and another is mentioned here

How hide hand cursor from Java buttons?

From this reference, i understood that:

java code has its own management to some cursor's, and then in this case "hand" cursor is different of system and have a special behavior.


Edition:

Extracted from reference for convenience:

Here is my problem:
I have a frame with a button in it.
On this button, I make a

bouton.setCursor(new Cursor(Cursor.HAND_CURSOR));

when I start my program, the frame has the default cursor (the arrow) and the button in the "hand".
On this frame I have a second button on which I have a listener.
When I click on this button I call on a library which modifies the Windows cursors by calling the method:

BOOL WINAPI SetSystemCursor(
_In_ HCURSOR hcur,
_In_ DWORD id
);

//OCR_HAND = 32649
SetSystemCursor(chemin_de_mon_image, 32649);

//OCR_NORMAL = 32512
SetSystemCursor(chemin_de_mon_image, 32512 );

...

So after having clicked on button 2, I have all the cursors on my Windows which are changed to "my image", even the "hand" cursor on Google links for example. On my Java application, all the cursor are changed except the "hand". Java doesn't seem to use wintow's native cursor for the "hand", but why? If anyone has a solution to this problem I am interested in it, or an explanation.



try to put something other than the hand to see if this new cursor is redrawn or not with your method. The idea is to see if it is the hand that has a special behavior or the cursor used for the bouron .....



Thank you for your ludomacho response. I tried with the other cursors (wait, cross, ...) and I manage to change the cursor, only the "main" cursor does not work. When we take a good look at the "main" cursor it looks like it is different from the "main" cursor of the system (no shading), while for the other cursors they are the same. There is a very simple test to do to find out, just create a frame with 2 buttons. On the first one we do: button1.setCursor (new Cursor (Cursor.WAIT_CURSOR)); On the second one we do: button2.setCursor (new Cursor (Cursor.HAND_CURSOR)); Then we go to the Windows settings and we modify the appearance of the "hand" and "wait" cursor. Only the "wait" cursor will have been modified in the application.



The java code has its own management of the "main" cursor.
To display the "main" cursor of the system (Windows Seven) you must modify the JRE and recompile it.

How to hide cursor in a Swing application?

It appears that the Cursor class does not have a "blank" cursor to begin with, so one could define a new "blank" cursor using the Toolkit.createCustomCursor method.

Here's one way I've tried which seems to work:

// Transparent 16 x 16 pixel cursor image.
BufferedImage cursorImg = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);

// Create a new blank cursor.
Cursor blankCursor = Toolkit.getDefaultToolkit().createCustomCursor(
cursorImg, new Point(0, 0), "blank cursor");

// Set the blank cursor to the JFrame.
mainJFrame.getContentPane().setCursor(blankCursor);

Edit

Regarding the comment about everything inside the JFrame ending up without a cursor, it seems that the Components which are contained in the JFrame will end up inheriting the cursor of the container (the JFrame), so if it is a requirement to have a certain Component have the cursor appear, one would have to manually set the desired cursor.

For example, if there is a JPanel contained in the JFrame, then one could set the cursor of that JPanel to the system's default using the Cursor.getDefaultCursor method:

JPanel p = ...
// Sets the JPanel's cursor to the system default.
p.setCursor(Cursor.getDefaultCursor());


Related Topics



Leave a reply



Submit