为什么即使在loadContent(...)之后Document也为null? - (WebView JavaFx) [英] Why Document is null even after loadContent(...)? - (WebView JavaFx)

查看:295
本文介绍了为什么即使在loadContent(...)之后Document也为null? - (WebView JavaFx)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是MainController类初始化(...)方法的简单代码:

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 

为什么 doc null 以及如何修复它?

Why doc is null and how to fix it?

推荐答案

正如我评论的那样,你应该添加一个监听器,因为加载需要时间,一旦执行内容已成功加载:

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

这篇关于为什么即使在loadContent(...)之后Document也为null? - (WebView JavaFx)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆