Set TextField width in JavaFX


Question

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.

1
8
11/7/2013 6:49:34 PM

Accepted Answer

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);
    }

}
15
11/7/2013 6:51:45 PM

Just set this methods after you create the TextField:

    TextField myTf = new TextField();
    myTf.setPrefWidth(80);
    myTf.setMaxWidth(80);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon