Custom cursor set in Javafx

I know that you can define the cursor through CSS as described in the JavaFX custom css cursor , or even set it globally as follows:

private void setCursor(Scene scene) {
    Dimension2D dim = ImageCursor.getBestSize(64, 64);
    URL url;
    if (dim.getWidth() > 32) {
        url = getClass().getResource("/icons/64/cursor.png");
    } else {
        url = getClass().getResource("/icons/32/cursor.png");
    }
    try {
        Image img = new Image(url.openStream());
        scene.setCursor(new ImageCursor(img));
    } catch (IOException e) {
        logger.warn("Failed to load cursor icon from {}", url);
    }
}

However, it sets the global cursor by default, while other types of cursor (e.g. carriage, processing) remain untouched. Is there a way to provide a JavaFX application with a cursor set that covers all cases?

In addition, with the above solution, the cursor is tied to the scene, which means that after it leaves the scene and, for example, hangs over the menu bar, it returns to the default value set in the OS. Is there a way to define the cursor at the scene or application level instead of the scene?

: . .

+4

Source: https://habr.com/ru/post/1666173/


All Articles