How can I set a JavaFX WebView as big as the scene?
Asked Answered
M

1

6

I have a JavaFX WebView which I want to have as big as the scene on start. If I could make it resize with the scene, this would be perfect.
But right now I focus just on the initial size.

@Override
public void start(Stage stage) {
    try {
        Scene scene = new Scene(createWebViewPane(), 1920, 1080);
        scene.getStylesheets().add(getClass().getResource("Styles/application.css").toExternalForm());
        stage.setScene(scene);
        stage.show();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

The above code creates a scene with width: 1920px, height: 1080px. createWebViewPane() creates a WebView on a pane.

private Pane createWebViewPane() {
    final Pane pane = new Pane();
    pane.minWidth(1920);
    pane.minHeight(1080);
    WebView browser = new WebView();
    browser.minWidth(1920);
    browser.prefWidth(1920);
    browser.minHeight(1080);
    browser.prefHeight(1080);
    WebEngine webEngine = browser.getEngine();
    webEngine.load(getClass().getResource("index.html").toExternalForm());
    pane.getChildren().add(browser);
    return pane;
}

There I try to set the width / height. I also set it in the stylesheet.

web-view{
    -fx-font-scale: 1;  
    -fx-min-width: 1920px;   
    -fx-min-height: 1080px;   
    -fx-pref-width: 100%; 
    -fx-pref-height: 100%;
}

But none of these properties seem to affect the WebView. It still has it's default size. 800px in width and 600px in height.

Mymya answered 20/11, 2015 at 9:52 Comment(3)
Try a StackPane, as shown here.Arak
Glad it helped. Rather than close this question as a duplicate, I added an answer specific to javafx; the other answer is more about swing.Arak
Jep, noticed it as well, but could adapt it :)Mymya
A
10

Add the WebView to a StackPane, which "will attempt to resize each child to fill its content area." Resize the frame to see how the StackPane reflows the WebView content based on the default Pos.CENTER.

image

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;

/** @see https://mcmap.net/q/1678297/-how-can-i-set-a-javafx-webview-as-big-as-the-scene */
public class WebViewPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        StackPane root = new StackPane();
        WebView webView = new WebView();
        WebEngine webEngine = webView.getEngine();
        webEngine.load("http://www.example.com");
        root.getChildren().add(webView);
        Scene scene = new Scene(root);
        primaryStage.setTitle("WebView");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}
Arak answered 20/11, 2015 at 10:18 Comment(1)
An updated variation may be found here.Arak

© 2022 - 2024 — McMap. All rights reserved.