Java Swing How to - Use Custom Cursor in a Swing JDialog








Question

We would like to know how to use Custom Cursor in a Swing JDialog.

Answer

import java.awt.Cursor;
/*from  w  ww  . jav a  2 s  .com*/
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

public class Main extends JFrame {
  private void ShowDialog() {
    JLabel label = new JLabel("Move mouse here for hand cursor");
    label.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    JOptionPane pane = new JOptionPane(label);
    pane.setOptions(new Object[] { "OK" });

    JDialog dialog = pane.createDialog(this, "Test Dialog");
    dialog.setVisible(true);
  }

  public static void main(String[] args) {
    Main testFrame = new Main();
    testFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    testFrame.setSize(500, 300);
    testFrame.setVisible(true);
    testFrame.ShowDialog();
  }
}