My goal is to have a JavaFX WebView that automatically resizes to fit the scene upon startup. Currently, I am focused on setting the initial size properly.
@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();
}
}
This code snippet initializes a scene with dimensions of 1920px width and 1080px height. The method createWebViewPane()
creates a pane containing a WebView.
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;
}
In this function, I attempt to set the width and height properties of the WebView. Additionally, I define these values 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%;
}
Despite specifying these properties, the WebView retains its default size of 800px width and 600px height.