4

In JavaFX, how do I create a textfield inside an hbox (BorderPane layout) resize in width/length as the user resizes the window?

asked Jun 23, 2015 at 20:53

1 Answer 1

11

You can set the HGROW for the textfield as Priority.ALWAYS.

This will enable the TextField to shrink/grow whenever the HBox changes its width.

MCVE :

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.stage.Stage;
public class Main extends Application {
 @Override
 public void start(Stage primaryStage) throws Exception {
 TextField textField = new TextField();
 HBox container = new HBox(textField);
 container.setAlignment(Pos.CENTER);
 container.setPadding(new Insets(10));
 // Set Hgrow for TextField
 HBox.setHgrow(textField, Priority.ALWAYS);
 BorderPane pane = new BorderPane();
 pane.setCenter(container);
 Scene scene = new Scene(pane, 150, 150);
 primaryStage.setScene(scene);
 primaryStage.show();
 }
 public static void main(String[] args) {
 launch(args);
 }
}

Output :

enter image description here

answered Jun 24, 2015 at 6:12
Sign up to request clarification or add additional context in comments.

1 Comment

That worked for me. How do I do the same for a textarea inside a scrollpane?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.