为什么即使在loadContent(…)之后文档也为空-(WebView JavaFx)

 2023-02-16    341  

问题描述

这是Main Controller类的简单代码初始化(…)方法:

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

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

为什么即使在loadContent(…)之后文档也为空-(WebView JavaFx)

推荐答案

正如我所评论的那样,您应该添加一个侦听器,因为加载需要时间,一旦内容成功加载:

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

其他推荐答案

有时即使在成功加载后,发动机即使将文档设置为无效.
这通常发生在更复杂的网页中.

确保文档不是null的更可靠的方法是使用属性侦听器.

import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import org.w3c.dom.Document;

class MyClass {

    private WebView view = new WebView();
    private WebEngine engine = view.getEngine();
    private Document document;

    MyClass() {
        engine.documentProperty().addListener((v, o, n) -> {
            if (n != null) {
                document = n;
            }
        });
    }
}

其他推荐答案

我浪费了很多时间来解决这个问题.
这是我的简单示例,通过单击按钮:

,它在警报窗口中显示了文本.

index.html

<!-- ... -->
<textarea id="inputText" rows="5" placeholder="Enter text"></textarea>
<!-- ... -->
<button id="goButton">Go!</button>
<!-- ... -->

maincontroller.java

public class MainController implements Initializable {
    @FXML private WebView webDoc;

    public void initialize(URL location, ResourceBundle resources) {
        final WebEngine webEngine = webDoc.getEngine();
        String url = this.getClass().getResource("/html/main.html").toExternalForm();

        webEngine.load(url);
        webEngine.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> {
            if (newState == Worker.State.SUCCEEDED) {

                final Document doc = webEngine.getDocument();

                EventTarget button = (EventTarget) doc.getElementById("goButton");

                button.addEventListener("click", evt -> {
                    HTMLTextAreaElement textField = (HTMLTextAreaElement) doc.getElementById("inputText");
                    alert(textField.getValue());
                }, false);
            }
        });
    }

    private void alert(String text) {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, text);
        alert.showAndWait();
    }
}

main.java

public class Main extends Application {

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

    @Override
    public void start(Stage stage) throws Exception {
        Scene scene = gecSceneFromXml("/fxml/main.fxml");

        stage.setTitle("New window");
        stage.setScene(scene);

        stage.show();
    }

    private Scene gecSceneFromXml(String fileName) throws java.io.IOException {
        FXMLLoader loader = new FXMLLoader();
        Parent root = loader.load(getClass().getResourceAsStream(fileName));
        return new Scene(root);
    }
}

main.fxml

<AnchorPane id="AnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400" prefWidth="500" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controller.MainController">
   <children>
      <WebView fx:id="webDoc" maxHeight="-Infinity" maxWidth="-Infinity"  prefHeight="400.0" prefWidth="500.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
   </children>
</AnchorPane>

以上所述是小编给大家介绍的为什么即使在loadContent(…)之后文档也为空-(WebView JavaFx),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对77isp云服务器技术网的支持!

原文链接:https://77isp.com/post/33954.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。