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
noobProgrammer
4773 gold badges8 silver badges20 bronze badges
1 Answer 1
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
ItachiUchiha
36.9k11 gold badges127 silver badges181 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
noobProgrammer
That worked for me. How do I do the same for a textarea inside a scrollpane?
lang-java