CursorTest.java

Download source code from src.zip

See CursorList.java for a list of predefined cursors.

No example image because it is difficult to capture a screen shot showing the cursor.


CursorTest.java

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


class TestPanel extends JPanel implements ItemListener
{
    JPanel top;
	JComboBox<String> cursors;
	Cursor custom;
	
	TestPanel()
	{
	
		//Create and load the duke icon.
		ImageIcon icon = createImageIcon("dukeWaveRed.gif");
		custom = Toolkit.getDefaultToolkit().createCustomCursor(icon.getImage(), new Point(15,15),
			"Duke Waving");

		
		String names[] = new String[15];
		int i;
		for (i=0; i<20; i++) {
			try {
				names[i] = Cursor.getPredefinedCursor(i).getName();
			}
			catch(IllegalArgumentException e)
			{
				break;
			}
		}
		System.out.println("There are " + i + " predefined cursors");
		names[14] = custom.getName();
		cursors = new JComboBox<String>(names);
		cursors.addItemListener(this);
		
		top = new JPanel();
		top.add(cursors);		
	}

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = TestPanel.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        }
	else return new ImageIcon(path);
    }

	public void itemStateChanged(ItemEvent e)
	{
		int idx = cursors.getSelectedIndex();
		setCursor( idx<14? Cursor.getPredefinedCursor( idx ): custom );
	}
	
}


public class CursorTest extends JApplet
{
    public void init()
    {
	TestPanel panel = new TestPanel();
	add(panel);
	add(panel.top,BorderLayout.NORTH);
    }

    public static void main( String args[] )
    {
	TestPanel panel = new TestPanel();
	JFrame frame = new JFrame("Cursor Test" );
	frame.add(panel);
	frame.add(panel.top,BorderLayout.NORTH);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setSize( 240, 180 );
	frame.setVisible(true);
    }
}


Maintained by John Loomis, updated Tue Feb 26 12:01:49 2019