Show different types of Cursor

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

Show different types of 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 org.eclipse.swt.graphics. 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 fileDialog.open() 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 CreateCursorExample.java

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
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");
  shell.setSize(320,150);
  shell.open();

  final Cursor cursor1 = new Cursor(display, SWT.CURSOR_HAND);
  final Button b1 = new Button(shell,SWT.PUSH);
  b1.setBounds(40508020);
  b1.setText("Hand Cursor");
  b1.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event e) {
  b1.setCursor(cursor1);
  }
  });
  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.setBounds(120508020);
  b2.setText("Source Cursor");
  b2.addListener(SWT.Selection, new Listener() {
  public void handleEvent(Event e) {
  b2.setCursor(cursor2);
  }
  });
  final Cursor[] cursor3 = new Cursor[1];
  final Button b3 = new Button(shell, SWT.PUSH);
  b3.setBounds(200508020);
  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",
  "*.jpg","*.bmp"});
  String image = fileDialog.open();
  if (image == null)
  return;
  ImageData imageData = new ImageData(image);
  Cursor cursor = cursor3[0];
  cursor3[0] = new Cursor(display, imageData, 00);
  shell.setCursor(cursor3[0]);
  if (cursor != null)
  cursor.dispose();
  }
  });
  while (!shell.isDisposed()) {
  if (!display.readAndDispatch())
  display.sleep();
  }
  cursor1.dispose();
  cursor2.dispose();
  cursor3[0].dispose();
  display.dispose();
  }
}

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