Home Java Example Java Awt Changing the Cursor in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Changing the Cursor in Java

Advertisement
In this section, you will see how to change the cursor in java.

Changing the Cursor in Java

     

Introduction

Here, you will learn how to change the mouse cursor in java. Changing the cursor means showing cursor in many different  shapes for the different objects.

First button is "Ok" button, when the cursor goes upon it, cursor shape changes to hand shape. If you move the mouse over "Cancel" button, the cursor shape changes to move cursor (MOVE_CURSOR).

In this process the following methods to be used in the program.

add(panel,BorderLayout.CENTER) :
The add() method has been used to add the panel to the frame. The BorderLayout container has the five components such as: NORTH, SOUTH, WEST, EAST and CENTER.

getCursor(): 
This method is used to declaring the predefined cursor to set for the object. This method contains the Cursor properties which indicates the several cursors.

setCursor() :
This method defined in the Component class. It changes the cursor icon when the cursor goes upon the particular component.

getPredefinedCursor() :
The getPredefinedCursor() method returns the cursor object to be specified in the cursor block.

Here is the code of the program :   

import java.awt.*;
import java.awt.event.*;

public class ChangeCursor{
  public static void main(String[] args) {
  Frame f = new Frame("Change cursor");
  Panel panel = new Panel();
  Button comp1 = new Button("Ok");
  Button comp2 = new Button("Cancel");
  panel.add(comp1);
  panel.add(comp2);
  f.add(panel,BorderLayout.CENTER);
  f.setSize(200,200);
  f.setVisible(true);
  Cursor cur = comp1.getCursor();
  Cursor cur1 = comp2.getCursor();
  comp1.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
  comp2.setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
  f.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });

  }
}

Download this example.

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 17, 2007

Ask Questions?    Discuss: Changing the Cursor in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
summit chandan
October 5, 2011
query

i want 2 know if the cursor setting is 24/2 or 24/7, what does it means and how it works...
Chanchal Singh
September 22, 2012
thanks for ur unlimted help and support.

I'm newbie in java, when i search any thing related to java i mostly see ur site, and i loved to open it. it teaches many things that i can even imagine. Great Great Great works
DMCA.com