Java WebView how to use

0

How do I use 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 within void because it will be called on a actionlistener of a button later.

    
asked by anonymous 07.12.2017 / 21:07

1 answer

1

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

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);
    }
}
    
10.12.2017 / 21:40