Search code examples
javafxfxmlscenebuilder

How to make multiple HBoxes fill the size of parent container with equal distribution in javaFX - FXML


I have two hboxes nested in a vbox and I want the height of all of them to be equally distributed within the vbox. I tried üsing computed in Scene Builder 8 but that's not getting me anywhere! Here's part of the code where I want the hboxes to be distributed evenly:

    <VBox prefHeight="269.0" prefWidth="1064.0"      
    BorderPane.alignment="CENTER">
     <BorderPane.margin>
        <Insets bottom="3.0" left="3.0" right="3.0" top="3.0" />
     </BorderPane.margin>
     <children>
        <HBox prefHeight="100.0" prefWidth="200.0" />
        <HBox prefHeight="100.0" prefWidth="200.0" />
     </children>
  </VBox>

Solution

  • You can add the VGrow property to the HBox and set it to ALWAYS. This will result in both the HBox always filling up the available space.

    <children>
      <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
      <HBox prefHeight="100.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
    </children>