Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Document is null even after loadContent(...)? - (WebView JavaFx)

Here is the simple code of MainController class initialize(...) method:

WebEngine webEngine = webView.getEngine();
webEngine.loadContent("<h1>hello</h1>"); // Successfully loaded on form
Document doc = webEngine.getDocument(); // null 

Why doc is null and how to fix it?

like image 691
Letfar Avatar asked Mar 15 '23 17:03

Letfar


1 Answers

As I commented, you should add a listener, as loading takes time, to execute once the content is successfully loaded:

final WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
webEngine.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> {
    if (newState == State.SUCCEEDED) {
        Document doc = webEngine.getDocument();
    }
});
webEngine.loadContent("<h1>hello</h1>");
//webEngine.load("http://google.ch"); // This works too
like image 134
Psychokiller1888 Avatar answered Mar 17 '23 05:03

Psychokiller1888