Tutorial : How To Change The Cursor in JavaFX

At IDRSolutions we are currently in the process of Developing a JavaFX implementation of our PDFViewer. In previous JavaFX articles I have shown How to Create Stacked Menus, How to Setup Key Combinations and How to Create a Border Glow Effect all in JavaFX.

In this tutorial we will be learning how to change the default mouse cursor to different styles and how to get a wait cursor for long operations.

By default, the cursor will be set to a simple pointer. To manipulate the cursor you can access it via the scene using :

scene.setCursor(Cursor.type);
scene.getCursor();

Change the Cursor When Entering a Button Area.

I will assume you have a basic understanding of JavaFX and how to setup and add to a Stage.

First we’re going to code in a MouseEvent which will detect whether the cursor has entered the Buttons area, if it has then we’ll change the cursor to a pointing hand.

hoverwindow

button.setOnMouseEntered(new EventHandler() {
    public void handle(MouseEvent me) {
        scene.setCursor(Cursor.HAND); //Change cursor to hand
    }
});

Change the Cursor When Exiting a Button Area.

Next, we’re going to code in a MouseEvent which will detect whether the cursor has exited the Buttons area, if it has then we’ll change the cursor to a crosshair.

crosshairwindow

button.setOnMouseExited(new EventHandler() {
    public void handle(MouseEvent me) {
        scene.setCursor(Cursor.CROSSHAIR); //Change cursor to crosshair
    }
});

Change the Cursor for Long Operations / Tasks.

Finally, we’re going to code in an ActionEvent which will trigger when we press the button. This will change the cursor to a wait cursor, perform the long task, then change the cursor back to its default.

loading window

button.setOnAction(new EventHandler() {
    @Override
    public void handle(ActionEvent event) {
        Task task = new Task() {
            @Override
            protected Integer call() throws Exception {
                int iterations;
                scene.setCursor(Cursor.WAIT); //Change cursor to wait style
                for (iterations = 0; iterations < 100000; iterations++) {
                    System.out.println("Iteration " + iterations);
                }
                scene.setCursor(Cursor.DEFAULT); //Change cursor to default style
                return iterations;
            }
        };
        Thread th = new Thread(task);
        th.setDaemon(true);
        th.start();
    }
});

Change the Cursor to a Custom Batman Cursor

For a bit of fun, we’re going to customise our cursor by setting our Cursor to an ImageCursor and pass in a Batarang image.

batman

Image image = new Image("batman.png");  //pass in the image path
scene.setCursor(new ImageCursor(image));

 

With some hope you can now change your own mouse cursors in your JavaFX Applications and have learnt something new! Please let us know below if you have anything to add or wish to leave a comment.

For the the code/class we have written in this tutorial please see here : CursorExample

You may find some of my other JavaFX articles of interest :

How to Setup Key Combinations in JavaFX
How to Create Stacked Menus in JavaFX
How to Create a Border Glow Effect in JavaFX.

We also have a JavaFX PDF Viewer plugin for NetBeans which you can grab here.

This post is part of our “JavaFX article Index” series. In these articles, we aim to explore the world of Java and Javafx. Have a look through!

If you’re a first-time reader, or simply want to be notified when we post new articles and updates, you can keep up to date by social media (TwitterFacebook and Google+) or the Blog RSS.

Ebook Page Link

The following two tabs change content below.
Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

Related Posts:

Nathan

About Nathan Howard

Nathan is a developer at IDRSolutions, focusing mainly on the development of HTML5 and Form support for the online PDF converter. He was a speaker and exhibitor at JavaOne 2013, co-presenting a session titled 'Lessons Learned from Using GlassFish with NetBeans". Nathan also enjoys writing technical blog-articles.

One thought on “Tutorial : How To Change The Cursor in JavaFX

  1. […] Howard has posted about how to change the cursor in JavaFX to a custom […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>