首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >追加文本WebEngine - JavaFX

追加文本WebEngine - JavaFX
EN

Stack Overflow用户
提问于 2012-12-05 17:13:17
回答 3查看 7.3K关注 0票数 4

如何将文本追加到text引擎?我试过这个:

代码语言:javascript
复制
public TabMessage(String title) {
    super(title);
    view = new WebView();
    engine = view.getEngine();
    engine.loadContent("<body></body>");
    view.setPrefHeight(240);
}

private void append(String msg){
    Document doc = engine.getDocument();
    Element el = doc.getElementById("body");
    String s = el.getTextContent();
    el.setTextContent(s+msg);
}

但是文档是null

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-12-06 02:54:32

代码语言:javascript
复制
final WebEngine appendEngine = view.getEngine();
btn.setOnAction(new EventHandler<ActionEvent>() {
 @Override public void handle(ActionEvent event) {
   appendEngine.executeScript(
     "document.getElementById('content').appendChild(document.createTextNode('World!'));"
   );
 }
});

我有时发现使用jQuery操作DOM比使用JavaScript或原生JavaScript DOM接口更简单。

代码语言:javascript
复制
final WebEngine appendEngine = view.getEngine();
btn.setOnAction(new EventHandler<ActionEvent>() {
 @Override public void handle(ActionEvent event) {
   executejQuery(appendEngine, "$('#content').append('World!');");
 }
});

...

private static Object executejQuery(final WebEngine engine, String script) {
  return engine.executeScript(
    "(function(window, document, version, callback) { "
    + "var j, d;"
    + "var loaded = false;"
    + "if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {"
    + " var script = document.createElement(\"script\");"
    + " script.type = \"text/javascript\";"
    + " script.src = \"http://code.jquery.com/jquery-1.7.2.min.js\";"
    + " script.onload = script.onreadystatechange = function() {"
    + " if (!loaded && (!(d = this.readyState) || d == \"loaded\" || d == \"complete\")) {"
    + " callback((j = window.jQuery).noConflict(1), loaded = true);"
    + " j(script).remove();"
    + " }"
    + " };"
    + " document.documentElement.childNodes[0].appendChild(script) "
    + "} "
    + "})(window, document, \"1.7.2\", function($, jquery_loaded) {" + script + "});"
  );
}

无论您是像Uluk那样使用Java Document API,还是使用JavaScript或JQuery API,Uluk优秀答案中的所有其他要点仍然适用。

票数 3
EN

Stack Overflow用户

发布于 2012-12-05 18:06:08

首先,如果webengine无法加载内容,或者在内容完全加载之前调用engine.getDocument(),文档将返回null。

代码语言:javascript
复制
package demo;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Demo extends Application {

    private WebView view;
    private WebEngine engine;

    @Override
    public void start(Stage primaryStage) {

        view = new WebView();
        engine = view.getEngine();
        engine.loadContent("<body><div id='content'>Hello </div></body>");
        view.setPrefHeight(240);

        Button btn = new Button("Append the \"World!\"");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                append(" \"World!\"");
            }
        });

        StackPane root = new StackPane();
        root.getChildren().addAll(view, btn);
        Scene scene = new Scene(root, 300, 250);

        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void append(String msg) {
        Document doc = engine.getDocument();
        Element el = doc.getElementById("content");
        String s = el.getTextContent();
        el.setTextContent(s + msg);
    }

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

请注意,我在主体中放置了一个带有id=content的div,因此doc.getElementById("content")将返回该div。

票数 6
EN

Stack Overflow用户

发布于 2018-02-06 15:12:27

虽然答案很古老,而且已经被接受,但我还是把我的发现放在了这里。

诀窍是在控制器的初始化方法中调用engine.loadContent,然后尝试将内容附加到某些操作上,如示例btn.setOnAction()中所述。通过这种方式,当您触发操作时,页面已经加载。如果我将用于加载的代码放在setOnAction()本身中,我得到的文档为空。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13719669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档