DEV Community

Cover image for TextField
Paul Ngugi
Paul Ngugi

Posted on

TextField

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.

Image description

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

Image description

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.

Image description

Image description

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 ******.

Top comments (0)