Warm tip: This article is reproduced from stackoverflow.com, please click
fxml java javafx pane

JavaFX: How to make a VBox and it's contents expand infinitely with window height like they do with

发布于 2020-05-27 17:26:33

Probably a very simple question but I have not been able to figure it out.

I have a ScrollPane (feat. Label) inside a VBox, inside a SplitPane:

(Full fxml file at the bottom)

enter image description here

When you expand the window or the split pane seperator horizontally, the Vbox automatically stretches to fit, the label re-centers appropriatelly, and the scroll pane expands to fit the vbox. This does not happen when epxanding vertically, and I would like it to. How can I achieve that? If there is a different container I should be using instead, please do tell.

Gif of my troubles if it helps: enter image description here Full fxml file:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane prefHeight="600.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="main.java.ui.DefaultLayoutController">
   <children>
      <BorderPane prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <top>
            <MenuBar BorderPane.alignment="CENTER">
              <menus>
                <Menu mnemonicParsing="false" text="File">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Close" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Edit">
                  <items>
                    <MenuItem mnemonicParsing="false" text="Delete" />
                  </items>
                </Menu>
                <Menu mnemonicParsing="false" text="Help">
                  <items>
                    <MenuItem mnemonicParsing="false" text="About" />
                  </items>
                </Menu>
              </menus>
            </MenuBar>
         </top>
         <center>
            <SplitPane dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0" BorderPane.alignment="CENTER">
              <items>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
                     <children>
                        <VBox prefHeight="573.0" prefWidth="306.0" style="-fx-background-color: #ccbfb1;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                           <children>
                              <Label alignment="CENTER" maxWidth="Infinity" prefHeight="61.0" prefWidth="260.0" style="-fx-alignment: center; -fx-background-color: #e6d7c8;" text="Text" textAlignment="CENTER">
                                 <font>
                                    <Font name="AdobeDevanagari-Regular" size="51.0" />
                                 </font>
                              </Label>
                              <ScrollPane fx:id="mainScrollPane" prefHeight="559.0" prefWidth="453.0">
                                <content>
                                  <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="200.0" prefWidth="200.0" />
                                </content>
                              </ScrollPane>
                           </children>
                        </VBox>
                     </children>
                  </AnchorPane>
                <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="573.0" prefWidth="364.0">
                     <children>
                        <VBox prefHeight="573.0" prefWidth="396.0" style="-fx-background-color: #e6c896;" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
                     </children>
                  </AnchorPane>
              </items>
            </SplitPane>
         </center></BorderPane>
   </children>
</AnchorPane>
Questioner
Gremious
Viewed
14
Ammar Tarajia 2020-03-12 12:15

One way you could achieve this is by setting the max height of the VBox and its elements to Double.MAX_VALUE using the setMaxHeight(double) method. Alternatively, you can use the static VBox.setVgrow(Priority) method (recommended as far as I'm aware) upon all of the VBox's children. You can iterate over the children just using a regular for loop:

for(Node child : yourBox.getChildren()) {
    VBox.setVgrow(child, Priority.ALWAYS);
}