UI Tutorial: Cursor Changes


Summary:

On many occasions, a program may take a varied amount of time to finish a calculation or process.  Because of this, it is necessary that the user be able to see that the program is working on something, and not just sitting there idle. One way to do this is to change the cursor icon to be an hour glass, or something else that fits better into the program. By doing this, it allows the user in a glance to see that the program is working.  Cursor changes may also be used when the user rolls over a certain part on the interface.  An example of this may be changing the cursor to a text cursor when it is over a text editable field, or to a cross when it is over a table. 

Where to Find It:

Location of the Java Libraries: http://www.cs.rit.edu/usr/local/jdk/docs/api/index.html

In the java libraries, you will be utilizing the setCursor method in the Component class, as well as the Cursor class.

The Basics:

To change a cursor from the normal arrow to a different cursor such as an hourglass, it is necessary to have and know the name of an applet that you would like to display the cursor on.  Once the App you are creating is to your liking, and you recognize the aspects of the program that should have cursor changes, you will use the setCursor method to change the mouse's icon.  If application is the app that you have created, the code you will put into place will look like this:

    application.setCursor(new Cursor(Cursor.WAIT_CURSOR));

In order for this to work correctly, you must import java.awt.Cursor.  Depending on your needs, you may wish to use other forms of a cursor.  The predefined cursors that you can use are defined in java.awt.Cursor:

       CROSSHAIR_CURSOR - The crosshair cursor type.

       CUSTOM_CURSOR - The type associated with all custom cursors.

       DEFAULT_CURSOR - The default cursor type (gets set if no cursor is defined).

       E_RESIZE_CURSOR - The east-resize cursor type.

       HAND_CURSOR - The hand cursor type.

       MOVE_CURSOR - The move cursor type.

       N_RESIZE_CURSOR - The north-resize cursor type.

       NE_RESIZE_CURSOR - The north-east-resize cursor type.

       NW_RESIZE_CURSOR - The north-west-resize cursor type.

       S_RESIZE_CURSOR - The south-resize cursor type.

       SE_RESIZE_CURSOR - The south-east-resize cursor type.

       SW_RESIZE_CURSOR - The south-west-resize cursor type.

       TEXT_CURSOR - The text cursor type.

       W_RESIZE_CURSOR - The west-resize cursor type.

       WAIT_CURSOR - The wait cursor type.

Customization:

          To change a cursor from one of the specified cursors above to a customized cursor, you will need to use

        createCustomCursor(Image cursor,Point hotSpot, String name)

method which returns a Cursor in the Toolkit class.  To set the custom cursor you must make an Image object that you would like the cursor to be.  You are then able to use the following line of code to make the cursor change. 

             application.setCursor(Toolkit.getDefaultToolkit().createCustomCursor(Image, new Point(0, 0), "Pointer"));
             //new Point(0,0) represents the hotspot..

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.) ( http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Java/Chapter12/cursors.html )

Best Practices:

Cursor changes are a way to signify to a user that a program is thinking, or it is time/and area to enter new information.  This can be particularly useful to a user that has never used your program before, and is not used to areas that may hang in the program.  Generally a user is able to figure out a program easier if the are simple visual indications of what the user is supposed to do.  If there is a hand, the user will know that they should click on the link; if there is an hourglass, the user knows they should wait.  Over all improving the usability of your program. 

Limitations:

When using the cursor changing functionality of java, there are very few limitations.  One includes the need for Java > version 1.2.  If using Java less than 1.2, the custom icon functionality of the toolkit is not supported. 

Things to Watch Out for When Including Cursor Changes

One area that may be an issues with implementing cursor changes is that the cursor you ma choose may be inappropriate to the action taking place.  If you choose an hourglass for an area that the user is to enter text, they will become confused, and  a program that is fully functional is no longer usable. 

References:

Other Helpful Resources: