Cursors

The cursor can be set programmatically.

CursorDemo example: (Run Applet)

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

public class CursorDemo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ListSelectionListener, ActionListener
    {
        private final String cursors[] =
        {
            "DEFAULT_CURSOR",
            "CROSSHAIR_CURSOR",
            "TEXT_CURSOR",
            "WAIT_CURSOR",
            "SW_RESIZE_CURSOR",
            "SE_RESIZE_CURSOR",
            "NW_RESIZE_CURSOR",
            "NE_RESIZE_CURSOR",
            "N_RESIZE_CURSOR",
            "S_RESIZE_CURSOR",
            "W_RESIZE_CURSOR",
            "E_RESIZE_CURSOR",
            "HAND_CURSOR",
            "MOVE_CURSOR"
        };
        private final JList cursorList = new JList(cursors);
        private final JButton cursorButton = new JButton("Hand");

        public View()
        {
            super();
            // add components to the applet panel
            this.add(this.cursorList);
            this.add(this.cursorButton);

            // add listeners to the components
            this.cursorList.addListSelectionListener(this);
            this.cursorButton.addActionListener(this);
        }

        @Override
        public void valueChanged(ListSelectionEvent e)
        {
            final int i = this.cursorList.getSelectedIndex();
            setCursor(Cursor.getPredefinedCursor(i));
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        }
    }
}

Custom Cursors

Any image can be used as a cursor.

Cursor_Custom_Demo example: (Run Applet)

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

public class Cursor_Custom_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel implements ActionListener
    {
        private Cursor magnifyingGlassCursor;
        private Cursor koalaBearCursor;
        private final JButton toggle = new JButton("Magnifying Glass");

        public View()
        {
            super();
            final Image magnifyingGlassImage = new ImageIcon(getClass().getClassLoader().getResource("images/magnifyingGlassIcon.png")).getImage();
            final Image koalaBearImage = new ImageIcon(getClass().getClassLoader().getResource("images/koala.jpg")).getImage();
            final Point point = new Point(0, 0); // hotpoint of the image

            try
            {
                // Crate two user defined Cursors
                magnifyingGlassCursor = Toolkit.getDefaultToolkit().createCustomCursor(magnifyingGlassImage, point, "Magnifying Glass");
                koalaBearCursor = Toolkit.getDefaultToolkit().createCustomCursor(koalaBearImage, point, "Koala Bear");
            }
            catch (Exception e)
            {
            }

            // add the toggle button onto the applet's panel
            this.add(this.toggle);
            this.toggle.addActionListener(this);
        }

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (this.toggle.getText().equals((String) "Magnifying Glass"))
            {
                this.setCursor(this.magnifyingGlassCursor);
                this.toggle.setText("Koala Bear");
            }
            else
            {
                this.setCursor(this.koalaBearCursor);
                this.toggle.setText("Magnifying Glass");
            }
        }
    }
}

Centering a Cursor's Hotpoint

Passing the width and height of the cursor image to the method

Toolkit.getDefaultToolkit().getBestCursorSize(width, height) 

will return the actual cursor size that the system will use. This information can be used to set the cursor's hotpoint. In the example below, the hotpoint is set to the centre of the cursor.

Cursor_Centre_Hotpoint_Demo example: (Run Applet)

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

public class Cursor_Centre_Hotpoint_Demo extends JApplet
{
    @Override
    public void init()
    {
        this.setContentPane(new View());
    }

    public class View extends JPanel
    {
        public View()
        {
            super();

            final JButton button = new JButton("A Button");
            final Image bullseyeImage = new ImageIcon(getClass().getClassLoader().getResource("images/bullseye.gif")).getImage();
            final Dimension cursorSize = Toolkit.getDefaultToolkit().getBestCursorSize(bullseyeImage.getWidth(this), bullseyeImage.getHeight(this));
            final Point point = new Point((int) (cursorSize.getWidth() / 2.0), (int) (cursorSize.getHeight() / 2.0));  // hotpoint of the image

            try
            {
                Cursor bullseyeCursor = Toolkit.getDefaultToolkit().createCustomCursor(bullseyeImage, point, "Bullseye");
                this.setCursor(bullseyeCursor);
            }
            catch (Exception e)
            {
            }

            // add a button onto the applet's panel
            this.add(button);
        }
    }
}
 
<div align="center"><a href="../versionC/index.html" title="DKIT Lecture notes homepage for Derek O&#39; Reilly, Dundalk Institute of Technology (DKIT), Dundalk, County Louth, Ireland. Copyright Derek O&#39; Reilly, DKIT." target="_parent" style='font-size:0;color:white;background-color:white'>&nbsp;</a></div>