Home : Course Map : Chapter 12 : Java :
Cursors
JavaTech
Course Map
Chapter 12

Printing
  Demo 1
Cursor Icons
  Demo 2
MouseButtons
  Demo 3
PopupMenu
  Demo 4
Keystrokes
  Demo 5
Audio
  Demo 6
Timing & Speed
  Demo 7
Toolkit
  Demo 8
AppletContext
  Demo 9
Exercises

    Supplements
Java Beans
More APIs
Java & Browsers
  Demo 1
     About JavaTech
     Codes List
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

It can be useful in designing a user interface to control the look of the cursor according to the current position and/or processing going on.

The AWT has the Cursor class that comes with 14 different cursor icons.

The Component class includes the setCursor() method that can change the cursor icon when it is above a particular component.

The applet below will display the 14 standard icons above the buttons.

CursorTestApplet

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

/** Demonstrate the different cursor styles.**/
public class CursorTestApplet extends JApplet
{
  CursorPanel fCursorPanel = null;

  /** Build the interface.**/
  public void init () {
    Container content_pane = getContentPane ();

    // Create an instance of a JPanel sub-class
    fCursorPanel = new CursorPanel ();

    // And add one or more panels to the JApplet panel.
    content_pane.add (fCursorPanel);
  }
} // class CursorTestApplet

/** A JPanel subclass showing the different cursors.**/
class CursorPanel extends JPanel
{
  CursorPanel () {

    setLayout (new GridLayout (7,2));

    JButton bt = new JButton ("Default");
    bt.setCursor (new Cursor (Cursor.DEFAULT_CURSOR));
    add (bt);

    bt = new JButton ("Busy");
    bt.setCursor (new Cursor (Cursor.WAIT_CURSOR));
    add (bt);

    bt = new JButton ("Hand");
    bt.setCursor (new Cursor (Cursor.HAND_CURSOR));
    add (bt);

    bt = new JButton ("Text");
    bt.setCursor (new Cursor (Cursor.TEXT_CURSOR));
    add (bt);

    bt = new JButton ("CrossHair");
    bt.setCursor (new Cursor (Cursor.CROSSHAIR_CURSOR));
    add (bt);

    bt = new JButton ("Move");
    bt.setCursor (new Cursor (Cursor.MOVE_CURSOR));
    add (bt);

    bt = new JButton ("East Resize");
    bt.setCursor (new Cursor (Cursor.E_RESIZE_CURSOR));
    add (bt);

    bt = new JButton ("North Resize");
    bt.setCursor (new Cursor (Cursor.N_RESIZE_CURSOR));
    add (bt);

    bt = new JButton ("West Resize");
    bt.setCursor (new Cursor (Cursor.W_RESIZE_CURSOR));
    add (bt);

    bt = new JButton ("South Resize");
    bt.setCursor (new Cursor (Cursor.S_RESIZE_CURSOR));
    add (bt);

    bt = new JButton ("NorthEast Resize");
    bt.setCursor (new Cursor (Cursor.NE_RESIZE_CURSOR));
    add (bt);

    bt = new JButton ("NorthWest Resize");
    bt.setCursor (new Cursor (Cursor.NW_RESIZE_CURSOR));
    add (bt);

    bt = new JButton ("SouthWest Resize");
    bt.setCursor (new Cursor (Cursor.SW_RESIZE_CURSOR));
    add (bt);

    bt = new JButton ("SouthEast Resize");
    bt.setCursor (new Cursor (Cursor.SE_RESIZE_CURSOR));
    add (bt);
  } // ctor

} // class CursorPanel

 

With Java 1.2 came the ability to create custom cursor icons. The method

   createCustomCursor(Image cursor, Point hotSpot, String name)

in the java.awt.Toolkit class, you pass the image for the cursor. The second argument specifies the so-called hotspot that sets the X, Y coordinates, relative to the top left corner of the image, of the pixel where the click occurs. For example, this would specify the end of the tip of an arrow cursor. The last argument provides the name of the cursor for the Java Accessibility system to use. (The Accessibility framework, not discussed here, provides for enhancements for handicapped users.)

 

References & Web Resources

Latest update: Dec. 6, 2004

              Tech
Tech APIs
Exercises

           Physics
Math/Science APIs
JAIDA Program
  Demo 1
Demo 1 Discussion
Exercises

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
21
22 23 24
Supplements

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.