【问题标题】:JavaFX webview, get document heightJavaFX webview,获取文档高度
【发布时间】:2013-03-22 02:07:49
【问题描述】:

如何在 JavaFx 中获取 webview 控件的文档高度?

【问题讨论】:

  • Java 和 Javascript 完全不同。另外,请扩展您的问题,也许您已经尝试过。
  • 我有一个 webview 控件,我想设置它的相对于文档高度的高度。
  • 请点击编辑更新您的问题。您的评论只是重新排列了原始问题中的单词!

标签: java javascript html javafx


【解决方案1】:

这就是你要找的:

Double.parseDouble(webView.getEngine().executeScript("document.height").toString())

【讨论】:

    【解决方案2】:

    您可以使用以下调用获取在 WebView 中显示的文档的高度:

    webView.getEngine().executeScript(
        "window.getComputedStyle(document.body, null).getPropertyValue('height')"
    );
    

    一个完整的应用来演示调用用法:

    import javafx.application.Application;
    import javafx.beans.value.*;
    import javafx.scene.Scene;
    import javafx.scene.web.*;
    import javafx.stage.Stage;
    import org.w3c.dom.Document;
    
    public class WebViewHeight extends Application {
      @Override public void start(Stage primaryStage) {
        final WebView webView = new WebView();
        final WebEngine engine = webView.getEngine();
        engine.load("http://docs.oracle.com/javafx/2/get_started/animation.htm");
        engine.documentProperty().addListener(new ChangeListener<Document>() {
          @Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
            String heightText = webView.getEngine().executeScript(
              "window.getComputedStyle(document.body, null).getPropertyValue('height')"
            ).toString();
            double height = Double.valueOf(heightText.replace("px", ""));    
    
            System.out.println(height);
          }
        });
        primaryStage.setScene(new Scene(webView));
        primaryStage.show();
      }
    
      public static void main(String[] args) { launch(args); }
    }
    

    以上答案来源:Oracle JavaFX forums WebView configuration thread


    相关功能的基于 Java 的 API 的相关问题跟踪器请求:

    RT-25005 Automatic preferred sizing of WebView.

    【讨论】:

    • 它有效,但结果不正确。我通过像素线测量,高度约为480px,代码返回414px。
    • 感谢您的分析。这个回复有点晚了。您的示例正在运行。但是我有一个嵌入在TableCell 中的webview。这里的结果总是 0 px(接近:stackoverflow.com/questions/22334983/…)关于如何解决这个问题的任何想法?
    • 请参阅:stackoverflow.com/questions/42856471/… 了解更具体的问题,包括运行示例。
    猜你喜欢
    • 2015-11-15
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 2018-01-01
    • 1970-01-01
    • 2015-06-02
    • 1970-01-01
    • 2016-08-25
    相关资源
    最近更新 更多