Java AWT | Cursor class with examples
Last Updated :
23 Oct, 2019
Cursor class is a part of Java AWT package and it is used to create custom cursors or inherit system or predefined cursors.
Cursor class is mainly used to encapsulate the bitmap representation of the mouse cursor.
Constructor of cursor class are :
- Cursor(int t) : Creates a cursor with specified class
- Cursor(String name) : Creates a custom cursor with specified name.
Commonly used methods
method |
explanation |
getDefaultCursor() |
return the system default cursor. |
getName() |
returns the name of this cursor. |
getPredefinedCursor(int t) |
returns a cursor object with the specified predefined type. |
getSystemCustomCursor(String n) |
returns a system-specific custom cursor object matching the specified name. |
getType() |
returns the type for this cursor |
toString() |
returns a string representation of this cursor. |
createCustomCursor(Image i, Point p, String name) |
create a custom cursor with a image and name specified . |
1. Program to apply some predefined and system cursors to components (label)
import java.awt.*;
import javax.swing.*;
class cursor extends JFrame {
static JFrame f;
static Label l, l1, l2;
cursor()
{
}
public static void main(String args[])
{
try {
f = new JFrame( "cursor" );
JPanel p = new JPanel();
l = new Label( "label one" );
l1 = new Label( "label two" );
l2 = new Label( "label three" );
Cursor c = new Cursor(CROSSHAIR_CURSOR);
Cursor c1 = new Cursor(HAND_CURSOR);
Cursor c2 = Cursor.getSystemCustomCursor( "Invalid.32x32" );
l.setCursor(c);
l1.setCursor(c1);
l2.setCursor(c2);
p.add(l);
p.add(l1);
p.add(l2);
f.add(p);
f.show();
f.setSize( 250 , 300 );
}
catch (Exception e) {
System.err.println(e.getMessage());
}
}
}
|
Output :
2. Program to add all predefined cursors to a choice
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class cursor extends JFrame implements ItemListener {
static JFrame f;
static Label l;
static Choice c;
cursor()
{
}
public static void main(String args[])
{
f = new JFrame( "cursor" );
JPanel p = new JPanel();
c = new Choice();
for ( int i = 0 ; i < 14 ; i++)
c.add(Cursor.getPredefinedCursor(i).getName());
cursor cu = new cursor();
l = new Label( " label one " );
c.addItemListener(cu);
p.add(l);
p.add(c);
f.add(p);
f.show();
f.setSize( 250 , 300 );
}
public void itemStateChanged(ItemEvent e)
{
l.setCursor(Cursor.getPredefinedCursor(c.getSelectedIndex()));
}
}
|
Output :
3. program to create a custom cursor and add it to labels
import java.awt.*;
import javax.swing.*;
class cursor extends JFrame {
static JFrame f;
static Label l, l1, l2;
cursor()
{
f = new JFrame( "cursor" );
JPanel p = new JPanel();
Image i = Toolkit.getDefaultToolkit().getImage( "f:\\gfg.jpg" );
Image i1 = Toolkit.getDefaultToolkit().getImage( "f:\\gfg.png" );
Point p11 = new Point( 0 , 0 );
l = new Label( "label one" );
l1 = new Label( "label two" );
Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(i, p11, "cursor1" );
Cursor c1 = Toolkit.getDefaultToolkit().createCustomCursor(i1, p11, "cursor2" );
l.setCursor(c);
l1.setCursor(c1);
p.add(l);
p.add(l1);
f.add(p);
f.show();
f.setSize( 250 , 300 );
}
public static void main(String args[])
{
cursor c = new cursor();
}
}
|
Output :
Note : The programs might not run in an online IDE please use an offline IDE.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...