URi
URi

Reputation: 67

WebView size in JavaFX Stage

I want to change the size of the Stage and WebView of a JavaFx application.

I can change the windows size but the browser size does not increases from 800px so the display of the html page does not fit all the windows.

It looks like this (note the light grey zone on the right):

enter image description here

This is some code of the page:

public class EfadosApp extends Application {
    private Scene scene;

    @Override public void start(Stage stage) {
        scene = new Scene(new Browser(), 1000,500);
        stage.setScene(scene);
        stage.show();
    }
}

class Browser extends Region {
    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        webEngine.load("www.oracle.com");
        getChildren().add(browser);
    }
}

Upvotes: 4

Views: 6009

Answers (1)

DVarga
DVarga

Reputation: 21799

You can simply fix this with extending for example StackPane rather than Region:

class Browser extends StackPane {
    ...
}

The difference is that Region resizes the children to their preferred size :

By default a Region inherits the layout behavior of its superclass, Parent, which means that it will resize any resizable child nodes to their preferred size, but will not reposition them. If an application needs more specific layout behavior, then it should use one of the Region subclasses: StackPane, HBox, VBox, TilePane, FlowPane, BorderPane, GridPane, or AnchorPane.

While StackPane tries to resize its children to fit the content area:

The stackpane will attempt to resize each child to fill its content area. If the child could not be sized to fill the stackpane (either because it was not resizable or its max size prevented it) then it will be aligned within the area using the alignment property, which defaults to Pos.CENTER.

This of course also means, if you use Region but either you set the preferred size of the WebView to "something big" like

browser.setPrefSize(5000, 5000);

or you bind the heightProperty and widthProperty of the WebView to the corresponding properties of the Stage,

Browser browser = new Browser();
browser.browser.prefHeightProperty().bind(stage.heightProperty());
browser.browser.prefWidthProperty().bind(stage.widthProperty());
scene = new Scene(browser, 1000, 500);

it will also work as you expect.

Upvotes: 4

Related Questions