Java Reference
In-Depth Information
To create a Cursor object representing a text cursor you could write:
Cursor myCursor = new Cursor(Cursor.TEXT _ CURSOR);
Alternatively you can retrieve a cursor of the predefined type using a static class method:
Cursor myCursor = Cursor.getPredefinedCursor(Cursor.TEXT _ CURSOR);
This method is particularly useful when you don't want to store the Cursor object, but just want to pass
it to a method, such as setCursor() for a Component object.
If you want to see what the standard cursors look like, you could add a cursor to the previous example,
along with the pink background:
Try It Out - Color and Cursors
We will change the background color of the content pane for the application window and try out a
different cursor. Make the following changes to TryWindow.java ; we will use the code we created
earlier that utilizes the toolkit:
import javax.swing.JFrame;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Cursor;
public class TryWindow
{
// The window object
static JFrame aWindow = new JFrame("This is the Window Title");
public static void main(String[] args)
{
Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size
// Set the position to screen center & size to half screen size
aWindow.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size
aWindow.setDefaultCloseOperation(JFrame.EXIT _ ON _ CLOSE);
aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR _ CURSOR));
aWindow.getContentPane().setBackground(Color.PINK);
aWindow.setVisible(true); // Display the window
}
}
You can try all the cursors by plugging in each of the standard cursor names in turn. You could also try
out a few variations on the background color.
Search WWH ::




Custom Search