JavaFX WebView: how do I change the default cursor?

Guillaume F. :

How do I change WebView's default cursor? Every change I make is ignored, the icon always reverts back to the default pointer.

Example:

import javafx.application.Application;
import javafx.scene.Cursor;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        webView.getEngine().loadContent("http://google.com");

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        scene.setCursor(Cursor.CLOSED_HAND); // Doesn't work, reverted to pointer
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

I also tried to change the webView cursor itself, but to no avail.

VGR :

The document’s HTML content defines the cursor, so you can modify the document’s body style after it loads:

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewCursorOverride extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    public void start(Stage primaryStage) {
        primaryStage.setTitle("JavaFX WebView Example");

        WebView webView = new WebView();
        webView.getEngine().getLoadWorker().stateProperty().addListener(
            (o, old, state) -> {
                if (state != Worker.State.SUCCEEDED) {
                    return;
                }

                Document doc = webView.getEngine().getDocument();
                Element body = (Element)
                    doc.getElementsByTagName("body").item(0);
                String style = body.getAttribute("style");
                body.setAttribute("style", "cursor: grab;" + style);
            });
        webView.getEngine().load("https://google.com");

        VBox vBox = new VBox(webView);
        Scene scene = new Scene(vBox, 960, 600);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

You can also create your own cursor from an image:

body.setAttribute("style",
    "cursor: url('https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Pixel_51_icon_cursor_click_top_right.svg/36px-Pixel_51_icon_cursor_click_top_right.svg.png') 27 9, default;" + style);

The full definition of the cursor CSS property is here. Here is the current list of predefined cursors; note that not all of them are supported on every system:

  • auto
  • default
  • none
  • context-menu
  • help
  • pointer
  • progress
  • wait
  • cell
  • crosshair
  • text
  • vertical-text
  • alias
  • copy
  • move
  • no-drop
  • not-allowed
  • grab
  • grabbing
  • e-resize
  • n-resize
  • ne-resize
  • nw-resize
  • s-resize
  • se-resize
  • sw-resize
  • w-resize
  • ew-resize
  • ns-resize
  • nesw-resize
  • nwse-resize
  • col-resize
  • row-resize
  • all-scroll
  • zoom-in
  • zoom-out

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=407276&siteId=1
Recommended