In this tutorial, we will illustrate how to change the mouse cursor in java.Here , changing the cursor means showing cursor in many different shapes for the different objects.In the example below,First button is "Ok" button, when the cursor goes upon it, cursor shape changes to hand shape. If you move the mouse over "Cancel" button, the cursor shape changes to move cursor (MOVE_CURSOR).The methods used in the program are add(panel,BorderLayout.CENTER), getCursor(),setCursor() and getPredefinedCursor().

The add() method has been used to add the panel to the frame. The BorderLayout container has the five components such as: NORTH, SOUTH, WEST, EAST and CENTER. The getCursor()method is used to declare the predefined cursor to set for the object. This method contains the Cursor properties which indicates the several cursors.The setCursor() method is defined in the Component class. It changes the cursor icon when the cursor goes upon the particular component. And the last getPredefinedCursor() method returns the cursor object to be specified in the cursor block.


Java Code: Program to the cursor in java program
 
import java.awt.*;
import java.awt.event.*;

public class ChangeCursor {

    public static void main(String[] args) {
        Frame f = new Frame("Change cursor");
        Panel panel = new Panel();
        Button comp1 = new Button("Ok");
        Button comp2 = new Button("Cancel");
        panel.add(comp1);
        panel.add(comp2);
        f.add(panel, BorderLayout.CENTER);
        f.setSize(200, 200);
        f.setVisible(true);
        Cursor cur = comp1.getCursor();
        Cursor cur1 = comp2.getCursor();
        comp1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        comp2.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent we) {
                System.exit(0);
            }
        });

    }
}