Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get JavaFX nodes (textarea, textfield) to resize correctly when user drags to resize the stage window?

Tags:

java

javafx

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:

before resize after resizing

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;");
like image 461
user2799603 Avatar asked Nov 30 '13 19:11

user2799603


People also ask

How do I make my GridPane resizable in JavaFX?

I've found the easiest way to do this is to create an HBox and add the GridPane and the VBox to it. Set the "fillHeight" property to true on the HBox. The "fillWidth" properties on the GridPane and VBox should be set to true, and their HGrow properties set to "always," (or "sometimes").

How to set button size in JavaFX?

Button SizeThe methods setMinWidth() and setMaxWidth() sets the minimum and maximum width the button should be allowed to have. The method setPrefWidth() sets the preferred width of the button. When there is space enough to display a button in its preferred width, JavaFX will do so.

What is TextArea in JavaFX?

public class TextArea extends TextInputControl. Text input component that allows a user to enter multiple lines of plain text. Unlike in previous releases of JavaFX, support for single line input is not available as part of the TextArea control, however this is the sole-purpose of the TextField control.


1 Answers

With a VBox, the components will take just enough space, vertically, to fit. After that, increase in size of the Stage does not make a difference.

Use BorderPane. If you have used Swing, this is like BorderLayout. This will let you place your components on the borders of the Stage and at the center and these components will stay where they are even after resizing.

SSCCE:

package stack;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

public class TextfieldAdjust extends Application {

    Scene scene;
    TextArea area;
    TextField field;
    BorderPane border;

    @Override
    public void start(Stage stage) throws Exception {
        border = new BorderPane();
        scene = new Scene(border);

        area = new TextArea();
        field = new TextField();

        area.setStyle("-fx-background-color: DARKGRAY;"
                + "-fx-text-fill: BLACK;"
                + "-fx-font-size: 14pt;");
        field.setStyle("-fx-background-color: WHEAT;"
                + "-fx-text-fill: BLACK;"
                + "-fx-font-size: 14pt;");

        border.setCenter(area);
        border.setBottom(field);

        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }
    public static void main(String[] args) {
        Application.launch("stack.TextFieldAdjust");
    }
}
like image 75
An SO User Avatar answered Oct 14 '22 09:10

An SO User