Share on Google+Share on Google+

Show different types of Cursor

This section illustrates you how to change the appearance of the cursor.

Show different types of Cursor


This section illustrates you how to change the appearance of the cursor.

Using SWT we can change the style of cursor, by using the class Cursor of package To show the hand cursor, we have defined the SWT.CURSOR_HAND in the constructor of Cursor. On clicking the Hand cursor button, the Event class is called which change the cursor using the method setCursor(cursor1).

To show the cursor which is created from the source, we have defined the class PaletteData. The method getSystemColor() returns the defined standard color. The class ImageData provides the description of the image. The method setPixel(i, j, 1) sets the pixel value and on clicking the button, a rectangular cursor will be appeared.

To show the cursor created from the selected image, we have used the class FileDialog to navigate the file system and select the file name. The method setFilterExtensions() set the file extensions which the dialog will use to filter the files. To make the dialog visible, the method is used. On clicking the image cursor button, the dialog is displayed to select the image. On selecting an image, the cursor shows the selected image. 

Here is the code of

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.widgets.Listener;

public class CreateCursorExample {
  public static void main(String[] args) {
  final Display display = new Display();
  final Shell shell = new Shell(display);
  shell.setText("Change Cursor");

  final Cursor cursor1 = new Cursor(display, SWT.CURSOR_HAND);
  final Button b1 = new Button(shell,SWT.PUSH);
  b1.setText("Hand Cursor");
  b1.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event e) {
  Color white = display.getSystemColor (SWT.COLOR_WHITE);
  Color black = display.getSystemColor (SWT.COLOR_BLACK);
  PaletteData palette = new PaletteData (new RGB [] {white.getRGB()
   , black.getRGB(),});

  ImageData sourceData = new ImageData (20201, palette);
  for (int i = 0; i < 10; i ++) {
  for (int j = 0; j < 20; j++) {
  sourceData.setPixel(i, j, 1);
  final Cursor cursor2 = new Cursor(display, sourceData, 1010);
  final Image source = new Image (display,sourceData);
  final Button b2 = new Button(shell, SWT.PUSH);
  b2.setText("Source Cursor");
  b2.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event e) {
  final Cursor[] cursor3 = new Cursor[1];
  final Button b3 = new Button(shell, SWT.PUSH);
  b3.setText("Image Cursor");
  b3.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event e) {
  FileDialog fileDialog = new FileDialog(shell);
  fileDialog.setFilterExtensions(new String[] { "*.ico""*.gif",
  String image =;
  if (image == null)
  ImageData imageData = new ImageData(image);
  Cursor cursor = cursor3[0];
  cursor3[0] = new Cursor(display, imageData, 00);
  if (cursor != null)
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())

Output will be displayed as:

On clicking the the first button, the hand cursor will be appeared. The second button displays the rectangular cursor and the last one shows the cursor creating from the image.

Download Source Code



Posted on: October 3, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

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.

Discuss: Show different types of Cursor   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image