Change Mouse Cursor - Java Swing

Java examples for Swing:Mouse Cursor

Description

Change Mouse Cursor

Demo Code

import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.UIManager;

public class Main extends JFrame {
  JButton harry, wade, hansel;//w  w w . j  av  a  2  s  .  c o  m

  public Main() {
    super("Choose a Cursor");
    setSize(400, 80);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setLayout(new FlowLayout());
    harry = new JButton("Crosshair");
    add(harry);
    wade = new JButton("Wait");
    add(wade);
    hansel = new JButton("Hand");
    add(hansel);
    // begin anonymous inner class
    ActionListener act = new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        if (event.getSource() == harry) {
          setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        }
        if (event.getSource() == wade) {
          setCursor(new Cursor(Cursor.WAIT_CURSOR));
        }
        if (event.getSource() == hansel) {
          setCursor(new Cursor(Cursor.HAND_CURSOR));
        }
      }
    };
    harry.addActionListener(act);
    wade.addActionListener(act);
    hansel.addActionListener(act);
    setVisible(true);
  }
  public static void main(String[] arguments) {
    new Main();
  }
}

Related Tutorials