Swing - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Swing Cursors

Swing Cursors

shape Introduction

Swing Cursors can be defined as point or indicator on the screen, it is used to select the input from the system that user operates with mouse. Three types of Swing Cursors are available in java collection framework to retrieve the elements from the collection. Enumeration is used to retrieve the objects one by one from the collection framework by elements method as follows. [java]Enumeration e=v.elements();[/java] Where v can be any vector object. Enumeration is only used for legacy classes and developer can perform only read operation. Iterator is used to overcome the limitations of the enumeration. It is universal cursor and developer can perform both read and remove operations.Creation of object to iterator is possible with iterator() as follows. [java]public Iterator iterator();[/java]Iterator is only for one direction cursor. ListIterator is used to overcome the limitations of both enumeration and iteration. In iterator and enumeration only forward direction of cursor is available where as in ListIterator both forward and backward directions are available.Replacement and addition of objects is possible in ListIterator to perform read and remove operations. In forward and backward direction it follows methods as follows. [java]public void next();//in forward direction. public void previous();//in backward directions.[/java]

Types of cursors

Different types of Swing Cursors are available in operating systems as follows.

Creating a Cursor

shape Example

The below example shows how to create a cursor by using Swing components. HowToCreateCursor.java [java]package swing; import java.awt.Cursor; import java.awt.Dimension; import java.awt.Toolkit; import javax.swing.JFrame; public class HowToCreateCursor { public static void main(String[] args) { JFrame aWindow = new JFrame("Create a Cursor"); Toolkit theKit = aWindow.getToolkit(); Dimension wndSize = theKit.getScreenSize(); 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.setVisible(true); } }[/java] Create the title of the window, size and include window tool kit to inherit the properties. [java]JFrame aWindow = new JFrame("Create a Cursor"); Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit Dimension wndSize = theKit.getScreenSize()[/java] Set the place of the application on the screen. [java]aWindow.setBounds(wndSize.width / 4, wndSize.height / 4, // Position wndSize.width / 2, wndSize.height / 2); // Size[/java] Include the cursor name in the method and display the method. [java]aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR)); aWindow.setVisible(true); // Display the window[/java] Output: Output will be as follows with cross hair cursor in the window.

Change the Cursor

shape Example

The below example shows how to change the shape of the cursor by extending JFrame. ChangeCursorShape.java [java]package swing;//create a package import java.awt.Cursor; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class ChangeCursorShape extends JFrame { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { ChangeCursorShape mainForm = new ChangeCursorShape(); mainForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainForm.setSize(250, 250); Cursor cursor = new Cursor(Cursor.HAND_CURSOR); mainForm.setCursor(cursor); mainForm.pack(); mainForm.setVisible(true); } }); } }[/java] Create a package and import all the required packages from Swing API. [java]package swing;//create a package import java.awt.Cursor; import javax.swing.JFrame; import javax.swing.SwingUtilities;[/java] Create a class that should extend the JFrame and invoke new runnable object to initialize the graphical user interface. [java]package swing;//create a package import java.awt.Cursor; import javax.swing.JFrame; import javax.swing.SwingUtilities; public class ChangeCursorShape extends JFrame { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() {[/java] Create object to the class and add size to the window with object. [java]ChangeCursorShape mainForm = new ChangeCursorShape(); mainForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mainForm.setSize(250, 250);[/java] Create any cursor like HAND CURSOR and set the cursor in object. [java]Cursor cursor = new Cursor(Cursor.HAND_CURSOR); mainForm.setCursor(cursor);[/java] Output: Output will be as follows with hand cursor in the window.

Wait Cursor

shape Example

This example shows how to create the redirecting cursor with swing components. WaitCursor.java [java]package swing; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class WaitCursor implements ActionListener { private JFrame frame; private String defaultButtonText = "Show Wait Cursor"; private JButton button = new JButton(defaultButtonText); private Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR); private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR); private boolean waitCursorIsShowing; public static void main(String[] args) { new WaitCursor(); } public WaitCursor() { // add actionlistener on the button button.addActionListener(this); frame = new JFrame(" Wait Cursor Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(button); frame.setPreferredSize(new Dimension(400,300)); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public void actionPerformed(ActionEvent evt) { if (waitCursorIsShowing) { waitCursorIsShowing = false; button.setText(defaultButtonText); frame.setCursor(defaultCursor); } else { waitCursorIsShowing = true; button.setText("Back to Default Cursor"); frame.setCursor(waitCursor); } } } [/java] Create a package and import all the required packages. [java]package swing; import java.awt.*; import java.awt.event.*; import javax.swing.*;[/java] Create a class that should implements the ActionListener and add title to the window, set the default button, set the default cursor. [java]public class WaitCursor implements ActionListener { private JFrame frame; private String defaultButtonText = "Show Wait Cursor"; private JButton button = new JButton(defaultButtonText); private Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR); private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR); private boolean waitCursorIsShowing;[/java] Add ActionListener on the button. [java]button.addActionListener(this);[/java] To change the cursor back to the default place. [java] waitCursorIsShowing = false; button.setText(defaultButtonText); frame.setCursor(defaultCursor);[/java] To change the cursor to the wait cursor mode. [java] waitCursorIsShowing = true; button.setText("Back to Default Cursor"); frame.setCursor(waitCursor);[/java] Output: Output will be as follows with wait icon is spinning. When click on Back to Default Cursor it comes to normal state.

Summary

shape Points

  • Cursor used to pic the single element from the multiple elements.
  • ListIterator is the powerful cursor.
  • Enumeration uses hasmoreelement(); method.