4

Webエンジンにテキストを追加するにはどうすればよいですか?私はこれを試しました:

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

4

3 に答える 3

6

engine.getDocument()まず、Webエンジンがコンテンツの読み込みに失敗した場合、またはコンテンツの読み込みが完全に完了する前に 呼び出した場合、Documentはnullを返します。

次に、doc.getElementById("body")IDが「body」のDOM要素を検索します。ただし、ロードされたコンテンツには、そのようなIDまたはIDはまったくありません。

これらをよりよく理解するには、実行可能な完全な例をここに示します。ボタンをクリックしてください。

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を返すことに注意してください。

于 2012-12-05T10:06:08.047 に答える
3

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!'));"
   );
 }
});

JavaドキュメントやネイティブのJavaScriptDOMインターフェイスよりも、jQueryを使用してDOMを操作する方が簡単な場合があります。

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のようにJavaDocumentAPIを使用する場合でも、JavaScriptまたはJQuery APIを使用する場合でも、Ulukの優れた回答の他のすべてのポイントが引き続き適用されます。

于 2012-12-05T18:54:31.613 に答える
0

答えは非常に古く、すでに受け入れられていますが、私はここに私の発見を置いています。

秘訣は、コントローラーのinitメソッドでengine.loadContentを呼び出してから、例btn.setOnAction()で説明されているようなアクションにコンテンツを追加しようとすることでした。このようにアクションを実行すると、ページはすでに読み込まれています。setOnAction()自体にロードするコードを入れると、ドキュメントがnullになります。

于 2018-02-06T07:12:26.560 に答える