finlx
finlx

Reputation: 89

JavaFX not showing full web page on 16:9 ratio

Heres the code:

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

public class Main extends Application{

public static void main(String[] args){

    Application.launch(args);

}

@Override

public void start(Stage stage) throws Exception{

    WebView myWebView = new WebView();
    WebEngine engine = myWebView.getEngine();
    engine.load("https://twitter.com/");

    VBox root = new VBox();
    root.getChildren().addAll(myWebView);

    Scene scene = new Scene(root, 1280, 720);
    stage.setScene(scene);

    stage.show();
}
}

If I run this it will show the web page on the top 3 quarters of the application window with a white box taking up the other quarter. It's even worse if I maximise the window, the site only takes up about half of the application window. Not sure whats causing this...

Upvotes: 0

Views: 199

Answers (1)

JKostikiadis
JKostikiadis

Reputation: 2917

You will need to make it fill the entire space on the Stage. Depending on your choice you will have the above cases.

Case 1 : Using a BorderPane.

By adding the BorderPane directly to the Stage and the WebView on the center of the BorderPane it will make the webView take all the available width and height of the Stage.

Example :

@Override
public void start(Stage stage) throws Exception {

    WebView myWebView = new WebView();
    WebEngine engine = myWebView.getEngine();
    engine.load("https://twitter.com/");

    BorderPane root = new BorderPane();
    root.setCenter(myWebView);

    Scene scene = new Scene(root);
    stage.setScene(scene);

    stage.show();
}

Case 2 : Using a VBox or HBox.

Those panes takes by default all the available width for VBox and height for HBox space and left the other dimension to be calculated by their children nodes. In order to utilize the full space you need to tell the node inside those panes to always grow ( vertical or horizontal depending of the pane )

Example :

@Override
public void start(Stage stage) throws Exception {

    WebView myWebView = new WebView();
    WebEngine engine = myWebView.getEngine();
    engine.load("https://twitter.com/");

    VBox root = new VBox(); // or HBox 
    root.getChildren().add(myWebView);

    // HBox.setHgrow(myWebView, Priority.ALWAYS);
    VBox.setVgrow(myWebView, Priority.ALWAYS);

    Scene scene = new Scene(root);
    stage.setScene(scene);

    stage.show();
}

There are a lot of other panes as well like AnchorPane ( which you will need to set its anchors ) or FlowPane etc and in some cases you will have to bind their prefWidthProperty and prefHeightProperty with the actual widthProperty/heightProperty of the Stage. But I believe you got the point.

Upvotes: 2

Related Questions