Setting TextField Width in JavaFX

How can I set the width of a TextField in JavaFX?

 TextField userTextField = new TextField(); 

I tried this:

 TextField userTextField = new TextField(); userTextField.setPrefWidth(80); 

But I do not see any changes.

+8
source share
3 answers

It works very well:

 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
source

Just set these methods after creating the TextField:

  TextField myTf = new TextField(); myTf.setPrefWidth(80); myTf.setMaxWidth(80); 
+1
source

I had the same problem (the way I got to this page) and I fixed it by putting a text box in the HBox. A problem may arise if the text field is only placed in the parent component, where the siblings are layout managers. For example, placing it in a GridLayout along with a VBox, HBox, or child GridLayout. Here is the code that did this;

 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 HBox to the root or other parent layout manager.

0
source

Source: https://habr.com/ru/post/957610/


All Articles