java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to change the cursor shape?

Using the following example you can change the shape of cursor in your Swing desktop application. Just create a cursor object and assign it to your JFrame instance. A collections of cursor shapes defined in the java.awt.Cursor class.

package com.javacoderanch.example.swing;

import javax.swing.*;
import java.awt.*;

public class CursorExample extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                CursorExample mainForm = new CursorExample();
                mainForm.setDefaultCloseOperation(
                    JFrame.EXIT_ON_CLOSE);
                mainForm.setSize(250, 250);

                // Here we create a hand shaped cursor!
                Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
                mainForm.setCursor(cursor);

                mainForm.pack();
                mainForm.setVisible(true);
            }
        });
    }
}