Search code examples
javafxfxml

TextArea inside VBox is not scaling vertically


I'm creating a simple GUI with JavaFX, but I stumbled upon a problem. Whenever I resize my GUI vertically. My TextAre doesn't resize itself, although it should fill the space.

MainClass

public class Main extends Application {

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

    @Override
    public void start(Stage primaryStage) {
        mainWindow(primaryStage);
    }

    private void mainWindow(Stage primaryStage) {
        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindow.fxml"));
            loader.setController(this);
            Parent root = loader.load();

            primaryStage.setScene(new Scene(root));
            primaryStage.setMinWidth(270);
            primaryStage.setMinHeight(450);
            primaryStage.show();
        } catch (IOException ignored) {}
    }
}

FXML:

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

<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.input.*?>
<?import javafx.scene.layout.*?>

<BorderPane minHeight="400.0" minWidth="240.0" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1"
            xmlns:fx="http://javafx.com/fxml/1">
   <center>
      <VBox>
         <children>
            <GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="110.0" prefWidth="240.0">

            </GridPane>
            <VBox>
               <children>
                  <Label text="Results:" />
                  <TextArea fx:id="resultArea" minHeight="200.0" VBox.vgrow="ALWAYS" />
               </children>
               <VBox.margin>
                  <Insets left="5.0" right="5.0" />
               </VBox.margin></VBox>
         </children>
      </VBox>
   </center>
</BorderPane>

What do I have to add, therewith my TextArea resizes itself vertically?


Solution

  • The main issue is that the VBox holding your TextArea is only going to take up as much space as it needs, so it does not expand when you resize the window.

    To fix that, you need to set the VGrow property of your VBox to ALWAYS, either in Scene Builder:

    screenshot

    Or your FXML:

    <VBox VBox.vgrow="ALWAYS">