JavaFX Custom Cursors

Setting custom cursors with JavaFx is relatively straightforward, but for one little catch. Your image has to be 32×32. If you want a 16×16 icon, simply leave the rest of the image transparent.

import java.awt.Toolkit;
import javafx.scene.Cursor;
import javafx.scene.image.Image;

public class ColorChooser extends Cursor
{
    public override function impl_getAWTCursor(): java.awt.Cursor {

        def url = "{__DIR__}eyedropper.png";

        var toolkit = Toolkit.getDefaultToolkit();

        var image = Image{width:32 height:32 url:url}.bufferedImage;
        var hotspot = new java.awt.Point(0,15);
        var cursor = toolkit.createCustomCursor(image, hotspot, "colorpicker");
        return cursor;
    }
}

public def ColorChooserCursor=ColorChooser{};

public function run()
{
    javafx.scene.shape.Rectangle
    {
        x:5 y:5
        width:200 height:200
        cursor:ColorChooserCursor
    }
}

Tags:

Bookmark and Share

You should follow me on twitter here

2 Responses to “JavaFX Custom Cursors”

  1. Emily Litella: All this custom cursing in JavaFX is terrible! There’s enough cursing as it is! - Use JavaFX writes:

    [...] to say, when I read the Chui’s Counterpoint blog post entitled JavaFX Custom Cursors, I had flashbacks to the Emily Litella SNL appearances (my favorite one being where she wondered [...]

  2. Java desktop links of the week, May 18 | Jonathan Giles writes:

    [...] posts how to get custom cursors in JavaFX , even when the image is smaller than the required [...]