A text field can be used to enter or display a string. TextField is a subclass of TextInputControl. Figure below lists the properties and constructors in TextField.
Here is an example of creating a noneditable text field with red text color, a specified font, and right horizontal alignment:
TextField tfMessage = new TextField("T-Strom");
tfMessage.setEditable(false);
tfMessage.setStyle("-fx-text-fill: red");
tfMessage.setFont(Font.font("Times", 20));
tfMessage.setAlignment(Pos.BASELINE_RIGHT);
When you move the cursor in the text field and press the Enter key, it fires an ActionEvent. The code below gives a program that adds a text field to the preceding example to let the user set a new message, as shown in Figure below.
TextFieldDemo extends RadioButtonDemo (line 9) and adds a label and a text field to let the user enter a new text (lines 14–21). After you set a new text in the text field and press the Enter key, a new message is displayed (line 24). Pressing the Enter key on the text field triggers an action event.
If a text field is used for entering a password, use PasswordField to replace TextField. PasswordField extends TextField and hides the input text with echo characters ******.