How to setup webview in javafx?

2.8k Views Asked by At
@FXML
void openCaptchaSolver(MouseEvent event) {
    Stage primaryStage = (Stage)((Node)event.getSource()).getScene().getWindow();
    Stage secondaryStage = new Stage();
    WebView web = new WebView();
    WebEngine engine = web.getEngine();
    engine.load("https://www.google.com");
    VBox root = new VBox();
    root.getChildren().addAll(web);
    secondaryStage.setScene(new Scene(root, 500, 575));
    secondaryStage.setTitle("Secondary Stage");
    secondaryStage.show();
}

I'm trying to get started with WebView's in JavaFX but when trying to open one I'm receiving the errors shown below, how would I go about fixing this issue?

Caused by: java.lang.IllegalAccessError: superclass access check failed: class com.sun.javafx.sg.prism.web.NGWebView (in unnamed module @0x6b0c2d26) cannot access class com.sun.javafx.sg.prism.NGGroup (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.sg.prism to unnamed module @0x6b0c2d26
2

There are 2 best solutions below

0
On BEST ANSWER

When getting any errors trying to run WebView make sure your VM options contain the module javafx.web.

VM Options: --module-path C:\path\to\javafx\openjfx-13.0.1_windows-x64_bin-sdk\javafx-sdk-13.0.1\lib --add-modules javafx.controls,javafx.fxml,javafx.web

In IntelliJ you can access the VM options by going to the "Edit Configurations" button in the top right of the IDE

0
On

It's late but for anyone who needs it, You just need at first add the lib to pom.xml:

<dependency> <groupId>org.openjfx</groupId><artifactId>javafx-web</artifactId> <version>17</version></dependency>

Then add: requires javafx.web to your module-info file. Build your app and you are ready to go!