如何在JavaFx中获取webview控件的文档高度?
答案 0 :(得分:5)
您可以使用以下调用获取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的相关问题跟踪器请求:
答案 1 :(得分:0)
这就是你要找的东西:
Double.parseDouble(webView.getEngine().executeScript("document.height").toString())