How to get JavaFX nodes (textarea, textfield) to resize correctly when user drags to resize the stage window?
I have a piece of code that creates a stage VBox with two nodes (TextArea, TextField). However, when the user drags to resize the window, these components are not dragged in proportion. Please see the pictures:
Here is my code, any suggestions on how to implement a fix so that the textfield is always at the bottom, and textarea expands to fill up the white space? Thanks!
Stage stage = new Stage();
VBox root = new VBox();
textArea = new TextArea();
textField = new TextField();
root.getChildren().addAll(textArea, textField);
textArea.setStyle("-fx-background-color: DARKGRAY;"
+ "-fx-text-fill: BLACK;"
+ "-fx-font-size: 14pt;");
textArea.setPrefSize(400, 316);
textArea.setEditable(false);
textArea.setWrapText(true);
textField.setStyle("-fx-background-color: DARKGRAY;"
+ "-fx-text-fill: BLACK;"
+ "-fx-font-size: 14pt;");
Best Answer
With a
VBox
, the components will take just enough space, vertically, to fit. After that, increase in size of theStage
does not make a difference.Use
BorderPane
. If you have used Swing, this is likeBorderLayout
. This will let you place your components on the borders of theStage
and at the center and these components will stay where they are even after resizing.SSCCE: