How I can set the width of a TextField
in JavaFX?
TextField userTextField = new TextField();
I tried this:
TextField userTextField = new TextField();
userTextField.setPrefWidth(80);
But I don't see any change.
How I can set the width of a TextField
in JavaFX?
TextField userTextField = new TextField();
I tried this:
TextField userTextField = new TextField();
userTextField.setPrefWidth(80);
But I don't see any change.
Works pretty fine:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
public class TextFieldWidthApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
TextField userTextField = new TextField();
userTextField.setPrefWidth(800);
primaryStage.setScene(new Scene(userTextField));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Just set this methods after you create the TextField:
TextField myTf = new TextField();
myTf.setPrefWidth(80);
myTf.setMaxWidth(80);
I had the same problem (that's how I landed on this page) and I fixed it by putting the textfield in a HBox. The problem might occur if the texfield is just put in a parent component alone where the siblings are layout managers. For example, putting it in a GridLayout alongside a VBox or HBox or a child GridLayout. Here is the code that did it;
HBox hbForTextField = new HBox();
TextField sample = new TextField();
sample.setAlignment(Pos.CENTER);//Align text to center
sample.setPrefWidth(120);//Set width
//Add the texfield to the HBox
hbForTextField.getChildren().addAll(generatedPassword);
You can then add the HBox to the root or other parent layout manager.