How do I change the cursor shape in Swing?

Using the following code snippet you can change the shape of mouse cursor in your Java Swing desktop application. The cursor is represented by the 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. In the snippet below, for demonstration, we change the cursor of the JFrame. Your mouse pointer or cursor shape will be changed if you positioned inside the frame.

A collections of cursor shape defined in the java.awt.Cursor class, such as:

  • Cursor.DEFAULT_CURSOR
  • Cursor.HAND_CURSOR
  • Cursor.CROSSHAIR_CURSOR
  • Cursor.WAIT_CURSOR
  • etc
package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Cursor;

public class ChangeCursor extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ChangeCursor frame = new ChangeCursor();
            frame.setTitle("Change Cursor");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(500, 500);

            // Here we create a hand shaped cursor!
            Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
            frame.setCursor(cursor);
            frame.setVisible(true);
        });
    }
}

How do I make a centered JFrame?

If you have a JFrame in your Java Swing application and want to center its position on the screen you can use the following code snippets.

The first way is to utilize java.awt.Toolkit class to get the screen size. The getScreenSize() method return a java.awt.Dimension from where we can get the width and height of the screen. Having this values in hand we can calculate the top left position of our JFrame as shown in step 2 of the code below.

package org.kodejava.swing;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;
import java.awt.Dimension;
import java.awt.Toolkit;

public class CenteredJFrame extends JFrame {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            // 1. Get the size of the screen
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

            CenteredJFrame frame = new CenteredJFrame();
            frame.setTitle("Centered JFrame");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(500, 500);

            // 2. Calculates the position where the CenteredJFrame
            // should be paced on the screen.
            int x = (screenSize.width - frame.getWidth()) / 2;
            int y = (screenSize.height - frame.getHeight()) / 2;
            frame.setLocation(x, y);
            frame.setVisible(true);
        });
    }
}

The second way which is better and simpler is to use the setLocationRelativeTo(Component) method. According to the Java API documentation of this method: If the component is null, or the GraphicsConfiguration associated with this component is null, the window is placed in the center of the screen.

If you call the JFrame.pack() method. The method should be called before the setLocationRelativeTo() method.

So we can rewrite the code above like this:

package org.kodejava.swing;

import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class CenteredJFrameSecond {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            CenteredJFrame frame = new CenteredJFrame();
            frame.setTitle("Centered JFrame");
            frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            frame.setSize(500, 500);

            // Place the window in the center of the screen.
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        });
    }
}