Java AWT | Cursor class with examples

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 classCursor(String name) : Creates a custom cursor with specified name.
Cursor(int t) : Creates a cursor with specified class
Cursor(String name) : Creates a custom cursor with specified name.
Commonly used methods
1. Program to apply some predefined and system cursors to components (label)
// Java  Program to apply some predefined and system cursors to components (label)import java.awt.*;import javax.swing.*;class cursor extends JFrame {    // frame    static JFrame f;      // label    static Label l, l1, l2;      // default constructor    cursor()    {    }      // main class    public static void main(String args[])    {        try {            // create a frame            f = new JFrame("cursor");              // create e panel            JPanel p = new JPanel();              // create labels            l = new Label("label one");            l1 = new Label("label two");            l2 = new Label("label three");              // create cursors            Cursor c = new Cursor(CROSSHAIR_CURSOR);            Cursor c1 = new Cursor(HAND_CURSOR);              // get System cursor            Cursor c2 = Cursor.getSystemCustomCursor("Invalid.32x32");              // set cursor            l.setCursor(c);            l1.setCursor(c1);            l2.setCursor(c2);              // add labels to panel            p.add(l);            p.add(l1);            p.add(l2);              // add panel to the frame            f.add(p);              // show the frame            f.show();            f.setSize(250, 300);        }        catch (Exception e) {            System.err.println(e.getMessage());        }    }}
Output :
// Java Program to add all predefined cursors to a choiceimport java.awt.*;import java.awt.event.*;import javax.swing.*;class cursor extends JFrame implements ItemListener {    // frame    static JFrame f;      // labels    static Label l;      // create a choice    static Choice c;      // default constructor    cursor()    {    }      // main class    public static void main(String args[])    {        // create a frame        f = new JFrame("cursor");          // create e panel        JPanel p = new JPanel();          // create a choice        c = new Choice();          // add items to choice        for (int i = 0; i < 14; i++)            c.add(Cursor.getPredefinedCursor(i).getName());          // object of class        cursor cu = new cursor();          // create a label        l = new Label(" label one ");          // add item listener to the choice        c.addItemListener(cu);          // add labels to panel        p.add(l);        p.add(c);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(250, 300);    }      // if an item of choice is selected    public void itemStateChanged(ItemEvent e)    {        // set the cursor        l.setCursor(Cursor.getPredefinedCursor(c.getSelectedIndex()));    }}
Output :
// Java program to create a custom cursor and add it to labelsimport java.awt.*;import javax.swing.*;class cursor extends JFrame {    // frame    static JFrame f;      // label    static Label l, l1, l2;      // default constructor    cursor()    {        // create a frame        f = new JFrame("cursor");          // create e panel        JPanel p = new JPanel();          // extract image        // the files gfg.jpg and gfg.png contains image of cursor        Image i = Toolkit.getDefaultToolkit().getImage("f:\\gfg.jpg");        Image i1 = Toolkit.getDefaultToolkit().getImage("f:\\gfg.png");          // point p        Point p11 = new Point(0, 0);          // create labels        l = new Label("label one");        l1 = new Label("label two");          // create cursors        Cursor c = Toolkit.getDefaultToolkit().createCustomCursor(i, p11, "cursor1");        Cursor c1 = Toolkit.getDefaultToolkit().createCustomCursor(i1, p11, "cursor2");          // set cursor        l.setCursor(c);        l1.setCursor(c1);          // add labels to panel        p.add(l);        p.add(l1);          // add panel to the frame        f.add(p);          // show the frame        f.show();        f.setSize(250, 300);    }      // main class    public static void main(String args[])    {        cursor c = new cursor();    }}
Output :
ManasChhabra2
java-swing
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
ArrayList in Java
Initializing a List in Java
Java Programming Examples
Convert a String to Character Array in Java
Convert Double to Integer in Java
Implementing a Linked List in Java using Class