Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 582
DYL Avatar asked Nov 25 '10 07:11

DYL


People also ask

How do I change my mouse pointer in Java?

awt. Cursor class. Create an instance of Cursor using the new operator and pass the cursor type to the Cursor class constructor. We can change Swing's objects ( JLabel , JTextArea , JButton , etc) cursor using the setCursor() method.

How do you change the cursor?

Customize Your Mouse on Windows 10Search for and click on “Mouse settings” on your computer via the Start button or the Search bar in your taskbar. In the Window that follows click on “Adjust mouse & cursor size” in the right-side column. The next window will offer options for changing the pointer size and color.

Can you change the shape of your cursor?

To do so, click the Start button, and then click Control Panel. Click Hardware and Sound, and then under Devices and Printers, click Mouse. In the Mouse Properties box, click on the Pointers and Pointer Options tab, and adjust the options to change the shape and size of your cursor by changing the “scheme”.


2 Answers

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

like image 126
Mohamed Saligh Avatar answered Oct 06 '22 15:10

Mohamed Saligh


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")); 
like image 35
stacker Avatar answered Oct 06 '22 15:10

stacker