Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

java - JavaFX webview, get document height

How can I get the document height for a webview control in JavaFx?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can get the height of a document displayed in a WebView using the following call:

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

A complete app to demonstrate the call usage:

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); }
}

Source of the above answer: Oracle JavaFX forums WebView configuration thread.


Related issue tracker request for a Java based API for a related feature:

RT-25005 Automatic preferred sizing of WebView.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...