Search code examples
javafxlayoutjavafx-bindings

JavaFX binding only applied after resizing the window


When I run the following code in the start method of my Main (JavaFX) class I get weird results. The window gets displayed but pane (with a green border) has a width of 0. It is supposed to have the same width as the container's height since I binded prefWidth to the height property. Then, when I resize the window, the binding comes into effect and the pane becomes a square. Notice that if I maximize the window it also doesn't apply the bindings.

Thank you!

//Create a pane with a min width of 10 and a green border to be able to see it
Pane pane = new Pane();
pane.setStyle("-fx-border-color: green; -fx-border-width: 2");

//Bind the pane's preferred width to the pane's height
pane.prefWidthProperty().bind(pane.heightProperty());

//Put the pane in a vbox that does not fill the stage's width and make the pane grow in the vbox
VBox container = new VBox(pane);
container.setFillWidth(false);
VBox.setVgrow(pane, Priority.SOMETIMES);

//Show the vbox
primaryStage.setScene(new Scene(container, 600, 400));
primaryStage.show();

Solution

  • The problem you are running into here is that when the container is laid out, it has no reasonable information as to the order in which it should compute the width and the height of the pane. So essentially what happens is it computes the width, which (since it's empty), is zero; then computes the height (which fills the container, since you told the VBox to do that). After that, the prefWidth property is changed, but by then the actual width has already been set, so it's essentially too late. The next time a layout pass occurs, the new pref width is taken into account.

    I haven't checked the actual layout code, but (since the default content bias is null) most likely the layout code for the vbox is going to do something equivalent to the following pseudocode:

    protected void layoutChildren() {
    
        // content bias is null:
        double prefWidth = pane.prefWidth(-1);
        double prefHeight = pane.prefHeight(-1);
    
        // no fill width:
        double paneWidth = Math.max(this.getWidth(), prefWidth);
        // vgrow, so ignore preferred height and size to height of the vbox:
        double paneHeight = this.getHeight();
        pane.resizeRelocate(0, 0, paneWidth, paneHeight);
    
    }
    

    The last call actually causes the height of the pane to change, which then causes the prefWidth to change via the binding. Of course, that's too late for the current layout pass, which has already set the width based on the previous preferred width calculation.

    Basically, relying on bindings to manage layout like this is not a reliable way of doing things, because you are changing properties (such as prefWidth in this example) during the layout pass, when it may be already too late to resize the component.

    The reliable way to manage layout for a pane like this is to override the appropriate layout methods, which are invoked by the layout pass in order to size the component.

    For this example, since the width depends on the height, you should return VERTICAL for the contentBias, and you should override computePrefWidth(double height) to return the height (so the width is set to the height):

    @Override
    public void start(Stage primaryStage) {
        Pane pane = new Pane() {
            @Override
            public Orientation getContentBias() {
                return Orientation.VERTICAL ;
            }
            @Override
            public double computePrefWidth(double height) {
                return height ;
            }
        };
        pane.setStyle("-fx-border-color: green; -fx-border-width: 2");
    
    
        //Bind the pane's preferred width to the pane's height
        //    pane.prefWidthProperty().bind(pane.heightProperty());
    
        //Put the pane in a vbox that does not fill the stage's width and make the pane grow in the vbox
        VBox container = new VBox(pane);
        container.setFillWidth(false);
        VBox.setVgrow(pane, Priority.SOMETIMES);
    
        //Show the vbox
        primaryStage.setScene(new Scene(container, 600, 400));
        primaryStage.show();
    }