2

In order to support oAuth 2 in my Swing application, I need to load a web page URL supplied by Google. Since JavaFX has a better web support, I decide to use the source code from http://docs.oracle.com/javafx/2/swing/SimpleSwingBrowser.java.htm

SwingUtilities.invokeLater(new Runnable() {

    public void run() {
        SimpleSwingBrowser browser = new SimpleSwingBrowser();
        browser.setVisible(true);
        browser.loadURL(googleUrl);
    }     
});

Since the code example provided by Oracle doesn't come with "auto-fit web page" feature, I will be getting a display something like

enter image description here

The closest example is http://java-no-makanaikata.blogspot.com/2012/10/javafx-webview-size-trick.html. However, the example requires us to inject our custom <div> tag into WebView's content, which is not something I wish to do. I prefer to HTML content loading, all done by `WebView.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

2

This works for me:

import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

public class WebViewHeightTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        String initialURL = "http://stackoverflow.com/questions/23170817/javafx-simpleswingbrowser-auto-fit-web-page" ;
        BorderPane root = new BorderPane();
        TextField locationBar = new TextField(initialURL);
        WebView webView = new WebView();
        WebEngine engine = webView.getEngine();

        locationBar.setOnAction(event -> engine.load(makeUrl(locationBar.getText())));

        root.setTop(locationBar);
        root.setCenter(webView);

            // Get full width and height of page when it's loaded:

        engine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> {
           if (newState == Worker.State.SUCCEEDED) {
               int width = (Integer) engine.executeScript("document.body.scrollWidth");
               int height = (Integer) engine.executeScript("document.body.scrollHeight");
               System.out.printf("[%d, %d]%n", width, height);
           }
        }); 

        engine.load(initialURL);

        Scene scene = new Scene(root, 600, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private String makeUrl(String text) {
        if (text.contains("://")) {
            return text ;
        } else {
            return "http://"+text ;
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Wow. Impressed. I realize I still need to add certain offset, to make it completely un-scroll-able `new Dimension(width + 16, height + 56)`. Any idea? I had tried to hide the scroll bar using http://stackoverflow.com/questions/11206942/how-to-hide-scrollbars-in-the-javafx-webview But, I can test whether the loaded page is scroll-able, by highlight the content and mouse drag. – Cheok Yan Cheng Apr 19 '14 at 14:54
  • 1
    Not sure I fully understand, but if you make the WebView large enough to hold the content, the scroll bars will be removed. If that javascript doesn't provide the right dimensions, experiment with some of the other javascript as listed in [this post](http://stackoverflow.com/questions/1145850/how-to-get-height-of-entire-document-with-javascript) – James_D Apr 19 '14 at 17:57