Swing - SPLessons

Swing Cursors

Chapter 18

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.

Enumeration e=v.elements();

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.

public Iterator iterator();

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.

public void next();//in forward direction.
public void previous();//in backward directions.

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

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); 
  }
}

Create the title of the window, size and include window tool kit to inherit the properties.

JFrame aWindow = new JFrame("Create a Cursor");
    Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
    Dimension wndSize = theKit.getScreenSize()

Set the place of the application on the screen.

aWindow.setBounds(wndSize.width / 4, wndSize.height / 4, // Position
        wndSize.width / 2, wndSize.height / 2); // Size

Include the cursor name in the method and display the method.

aWindow.setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    aWindow.setVisible(true); // Display the window

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

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);
      }
    });
  }
}

Create a package and import all the required packages from Swing API.

package swing;//create a package
 
import java.awt.Cursor;
 
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

Create a class that should extend the JFrame and invoke new runnable object to initialize the graphical user interface.

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() {

Create object to the class and add size to the window with object.

ChangeCursorShape mainForm = new ChangeCursorShape();
        mainForm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainForm.setSize(250, 250);

Create any cursor like HAND CURSOR and set the cursor in object.

Cursor cursor = new Cursor(Cursor.HAND_CURSOR);
        mainForm.setCursor(cursor);

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

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);
    }
  }
   
}

Create a package and import all the required packages.

package swing;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

Create a class that should implements the ActionListener and add title to the window, set the default button, set the default cursor.

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;

Add ActionListener on the button.

button.addActionListener(this);

To change the cursor back to the default place.

      waitCursorIsShowing = false;
      button.setText(defaultButtonText);
      frame.setCursor(defaultCursor);

To change the cursor to the wait cursor mode.

      waitCursorIsShowing = true;
      button.setText("Back to Default Cursor");
      frame.setCursor(waitCursor);

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.