Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set TextField width in JavaFX

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.

like image 713
Peter Penzov Avatar asked Nov 07 '13 18:11

Peter Penzov


People also ask

How to set width of Text Field in JavaFX?

JavaFX will determine the width of the text field based on a number of factors, including the size of the window that contains the stage and scene and any size constraints placed on the pane or panes that contain the text field. TextField text1 = new TextField(); text1. setMinWidth(150); text1. setMaxWidth(250); text1.

What is TextField in JavaFX?

TextField class is a part of JavaFX package. It is a component that allows the user to enter a line of unformatted text, it does not allow multi-line input it only allows the user to enter a single line of text. The text can then be used as per requirement.

How do you add margins in JavaFX?

You can set the margin for child nodes of a JavaFX VBox using the static setMargin() method. Here is an example of setting the margin around a JavaFX Button using the setMargin() method: Button button = new Button("Button 1"); VBox vbox = new VBox(button); VBox. setMargin(button, new Insets(10, 10, 10, 10));


2 Answers

Just set this methods after you create the TextField:

TextField myTf = new TextField();
myTf.setPrefWidth(80);
myTf.setMaxWidth(80);
like image 55
edilon Junior Avatar answered Sep 24 '22 06:09

edilon Junior


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

}
like image 26
Jens Piegsa Avatar answered Sep 23 '22 06:09

Jens Piegsa