TextField Overview

Man working on his laptop
Joshua Hodge Photography/E+/Getty Images

The TextField class is used to create a control that allows the user to enter in a single line of text. It supports having prompt text (i.e., text that informs the user what the TextField is meant to be used for).

Note: If you need a multi-line text input control then have a look at the TextArea class. Alternatively, if you want the text to be formatted then have a look at the HTMLEditor class.

Import Statement

import javafx.scene.control.TextField;

Constructors

The TextField class has two constructors depending on whether you want to create an empty TextField or one with some default text:

  • To create an empty TextField object:
    TextField txtFld= new TextField();
  • To create a TextField with some default text use a String literal:
    TextField txtFld = new TextField("Default Text");

Note: Creating a TextField with default text is not the same as having prompt text. The default text will remain in the TextField when the user clicks on it and when they do will be editable.

Useful Methods

If you create an empty TextField you can set the text using the setText method:

txtField.setText("Another String");

To get a String representing the text the user entered into a TextField use the getText method:

String inputText = txtFld.getText();

Event Handling

The default event associated with the TextField is the ActionEvent. This is triggered if the user hits ENTER whilst inside the TextField To set up the EventHandler for an ActionEvent use the setOnAction method:

txtFld.setOnAction(new EventHandler{
 @Override public void handle(ActionEvent e) {
 
 //Place the code you want to execute on the press of the ENTER key.
 
 }
});

Usage Tips

Take advantage of the ability to set prompt text for the TextField if you need to help the user understand what the TextField is for.

Prompt text appears in the TextField as slightly greyed out text. If the user clicks on the TextField the prompt text disappears and they have an empty TextField in which to input their own text. If the TextField is empty when it loses focus the prompt text will reappear. The prompt text will never be the String value returned by the getText method. 

Note: If you create a TextField object with default text then setting the prompt text will not overwrite the default text.

To set the prompt text for a TextField use the setPromptText method:

txtFld.setPromptText("Enter Name..");

To find out the value of the prompt text of a TextField object use the getPromptText method:

String promptext = txtFld.getPromptText();

It is possible to set a value for the number of characters a TextField will show. This is not the same as limiting the number of characters that can be entered into the TextField. This preferred column value is used when calculating the TextField's preferred width - it is only a preferred value and the TextField might become wider due to layout settings.

To set the preferred number of text columns use the setPrefColumnCount method:

txtFld.setPrefColumnCount(25);

To find out about other JavaFX controls have a look at JavaFX User Interface Controls.