Cursor
Introduced in Java 1.1, the Cursor
class provides the different cursors that can be associated with a Component
. Previously, cursors could only be associated with a whole Frame
. Now any component can use fancy cursors when the user is interacting with the system.
To change the cursor, a component calls its setCursor()
method; its argument is a Cursor
object, which is defined by this class.
NOTE:
There is still no way to assign a user-defined cursor to a Component
. You are restricted to the 14 predefined cursors.
Cursor Constants
The following is a list of Cursor
constants. The cursors corresponding to the constants are shown in Figure 5.6.
public final static int DEFAULT_CURSOR
public final static int CROSSHAIR_CURSOR
public final static int TEXT_CURSOR
public final static int WAIT_CURSOR
public final static int HAND_CURSOR
public final static int MOVE_CURSOR
public final static int N_RESIZE_CURSOR
public final static int S_RESIZE_CURSOR
public final static int E_RESIZE_CURSOR
public final static int W_RESIZE_CURSOR
public final static int NE_RESIZE_CURSOR
public final static int NW_RESIZE_CURSOR
public final static int SE_RESIZE_CURSOR
public final static int SW_RESIZE_CURSOR
Figure 5.6: Standard Java cursors
Cursor Methods
- public Cursor (int type)
- The sole constructor creates a
Cursor
of the specifiedtype
.type
must be one of theCursor
class constants. Iftype
is not one of the class constants, the constructor throws the run-time exceptionIllegalArgumentException
.This constructor exists primarily to support object serialization; you don't need to call it in your code. It is more efficient to call
getPredefinedCursor()
, discussed later in this section.
- public int getType()
- The
getType()
method returns the cursor type. The value returned is one of the class constants. - static public Cursor getPredefinedCursor(int type)
- The
getPredefinedCursor()
method returns the predefinedCursor
of the giventype
. Iftype
is not one of the class constants, this method throws the run-time exceptionIllegalArgumentException
. This method checks whatCursor
objects already exist and gives you a reference to a preexistingCursor
if it can find one with the appropriate type. Otherwise, it creates a newCursor
for you. This is more efficient than calling theCursor
constructor whenever you need one. - static public Cursor getDefaultCursor()
- The
getDefaultCursor()
method returns the predefinedCursor
for theDEFAULT_CURSOR
type.