Adam
Adam

Reputation: 36723

JavaFX scene does not resize to fill stage if stage has minWidth/minHeight set

I need to set a minimum width and minimum height on a given window.

I've found that if I do so then the Scene does not fill the parent when the window is first shown. If you manually stretch the window it repaints correctly.

This only appears to occur when minWidth = width AND minHeight = height. It all other cases it is resized as expected (say for example minWidth = width -1).

Additionally this only appears to occur on Linux, specifically Red Hat 6, and only on Java 8. I can't find any existing bug report for this behavior.

I'm looking for a workaround to this problem. This is a legacy project and unfortunately updating Java is not possible.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class SceneDoesntFillStage extends Application {

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

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setMinWidth(300);
        primaryStage.setWidth(300);

        primaryStage.setMinHeight(200);
        primaryStage.setHeight(200);

        HBox root = new HBox() {
            protected void layoutChildren() {
                super.layoutChildren();
                System.out.println("Layout");
            }
        };
        root.setStyle("-fx-background-color:red");
        root.setPrefWidth(250);
        root.setPrefHeight(150);
        primaryStage.setScene(new Scene(root));
        primaryStage.show();

        System.out.println(primaryStage.getScene().getWidth() + " x " + primaryStage.getScene().getHeight());
    }

}

1

Upvotes: 5

Views: 717

Answers (1)

Chus
Chus

Reputation: 195

The question is old, but anyway... You can try this one:

root.prefHeightProperty().bind(scene.heightProperty());
root.prefWidthProperty().bind(scene.widthProperty());

And also remove this:

root.setPrefWidth(250);
root.setPrefHeight(150);

Upvotes: 0

Related Questions