Using Custom Cursors in Java

This Java tutorial shows how to go beyond the predefined cursors in Java and create your own cursors using a GIF or PNG that are dsiplayed when the user moves the mouse over a Java component (AWT or Swing).

Creating the Cursor Image
For the custom cursor, a transparent GIF or PNG will be needed for the cursor. For this tutorial, a sample transparent GIF image of a pencil is provided below (right click to save):

Loading the Cursor Image
The image for the custom cursor needs to be loaded into an Image object.

//Get the default toolkit
Toolkit toolkit = Toolkit.getDefaultToolkit();

//Load an image for the cursor
Image image = toolkit.getImage("pencil.gif");
Defining the Cursor Hot Spot
The cursor hot spot is used for the point location in mouse events. For a cursor such as a cross-hair cursor, the hot spot would be in the center of the cursor. In our example, the cursor hot spot will be at the pencil point or the point 0,0.
//Create the hotspot for the cursor
Point hotSpot = new Point(0,0);
Creating the Custom Cursor
To create the custom image, we put together the cursor image and the hot spot:
//Create the custom cursor
Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "Pencil");

Displaying the Custom Cursor
The final step is to notify the component to display the cursor.
//Use the custom cursor


Anonymous said...

hehehe..that was great..brief and working

Priyanka said...

my application is build in swing.When i tried implementing the solution prosed in this blog, the cursor totally disappears in the application frame. ALso, the issue that i am facing is that in windows the wait cursor is the hourglass;now i want a similar icon or a different as available with MAC OS to appear when i run my application on MAC enviornment. Please can anyone suggest an approach for the same?

Anonymous said...

hey what do you need to import?

Anonymous said...

how i can change the size of my cursor?

Anonymous said...

tyvm! work perfectly, short, brief, thanks dude!!!!!!!11!!!one

Anonymous said...

Well.. It didn't work for me, I'm running a quite complicated Applet, and of course I inserted it in the init() method. And when I run my Applet (via. a HTML file). I get some acces denied error. Can you please help me?

Anonymous said...

Nice.. thanks a lot, took 2 seconds

ComputerNERD said...

About the Acces denied error, try importing the image using:

Image img = getImage(getCodeBase(),"Pencil.gif");

Java applets have security settings that don't really work with loading files in some ways.

Anonymous said...

It work's fine for me. Thanks.

Anonymous said...

Safe the images on the server then access it via a http URL for use in an applet. You can load it locally over the web anyway

Anonymous said...

the code is really cool... but the cursor is not visible...pls help... tyvm

Anonymous said...

I have also invisible cursor ... dn why...pls help

Anonymous said...

are these supposed to work in a javax.swing.JWindow? cuz its not working for me....

Michelle said...

Do I just paste all of this code in my file or do I need to create a separate js file??? THanks

Gizmo said...


Since you said JS file, are you meaning JavaScript file? This meant for Java GUI applications (AWT or Swing).

Post a Comment