Java Webview how to use

Asked

Viewed 1,243 times

0

How do I use the webview when I use:

public void site(){
    WebView browser = new WebView();
    WebEngine webEngine = browser.getEngine();
    webEngine.load("http://mySite.com");
}

I have the error:

    Exception in thread "AWT-EventQueue-0" java.lang.ExceptionInInitializerError
    at javafx.scene.web.WebEngine.<clinit>(WebEngine.java:315)
    at javafx.scene.web.WebView.<init>(WebView.java:273)

I need to use inside a void for she will be called in a actionlistener from a button further forward.

  • According to the documentation: WebView objects must be created and accessed solely from the FX thread.

1 answer

1

I couldn’t reproduce the problem. The problem may be in the way this method site is being called, or in how the method main is being mounted. Based on what you want to do in the future, I made an example that calls webview on onAction button:

public class WebViewExample extends Application {

    @Override
    public void start(Stage primaryStage) {
        VBox vbox = new VBox();
        Button button = new Button("Load");
        button.setOnAction((event) -> {
            WebView browser = new WebView();
            WebEngine webEngine = browser.getEngine();
            webEngine.load("http://mySite.com");
            vbox.getChildren().add(browser);
        });

        vbox.getChildren().add(button);

        Scene scene = new Scene(vbox,400,400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    public static void main(String[] args) {
        launch(args);
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.