Search code examples
javajavafxfxml

JavaFX Hbox of buttons won't fill space of bottom container in BorderPane


I am trying to fill the bottom container of a borderpane with buttons. According to the JavaFX documentation, the buttons should be set to the prefHeight and the width should fill the container. I set the all of the children to have a maxWidth of infinity, so I know the children's maxWidth is not blocking the resize. See below for fxml.

 <BorderPane>
        <top>
            <VBox>
                <Label text="This is a message"/>
                <HBox>
                    <TextField>Text 1</TextField>
                    <TextField>Text 2</TextField>
                    <TextField>Text 3</TextField>
                </HBox>
            </VBox>
        </top>

        <center>
            <TableView>

            </TableView>
        </center>

        <bottom>
            <HBox maxWidth="Infinity">
                <Button maxWidth="Infinity">Button 1</Button>
                <Button maxWidth="Infinity">Button 2</Button>
                <Button maxWidth="Infinity">Button 3</Button>
                <Button maxWidth="Infinity">Button 4</Button>
                <Button maxWidth="Infinity">Button 5</Button>
            </HBox>
        </bottom>

    </BorderPane>

Solution

  • hgrow will set the horizontal grow priority for the child node. The P

    <?xml version="1.0" encoding="UTF-8"?>
    
    <?import javafx.scene.control.*?>
    <?import javafx.scene.layout.*?>
    <BorderPane xmlns="http://javafx.com/javafx/8">
        <top>
            <VBox>
               <children>
                   <Label text="This is a message" />
                   <HBox>
                      <children>
                          <TextField>Text 1</TextField>
                          <TextField>Text 2</TextField>
                          <TextField>Text 3</TextField>
                      </children>
                   </HBox>
               </children>
            </VBox>
        </top>
    
        <center>
            <TableView>
    
            </TableView>
        </center>
    
        <bottom>
            <HBox maxWidth="Infinity">
               <children>
                   <Button maxWidth="Infinity" HBox.hgrow="ALWAYS">Button 1</Button>
                   <Button maxWidth="Infinity" HBox.hgrow="ALWAYS">Button 2</Button>
                   <Button maxWidth="Infinity" HBox.hgrow="ALWAYS">Button 3</Button>
                   <Button maxWidth="Infinity" HBox.hgrow="ALWAYS">Button 4</Button>
                   <Button maxWidth="Infinity" HBox.hgrow="ALWAYS">Button 5</Button>
               </children>
            </HBox>
        </bottom>
    
    </BorderPane>