JavaFX TextField Tutorial
The JavaFX Text Field is a text input control that allows the user to enter a single line of plain text. This node is useful when you’re requiring users to input a name or something else.
In this JavaFX Text Field tutorial, I will walk you through the creation of this node and show something different in the examples below. Since this child node only allows you to enter or input a single line of plain text, there is also a node that can be able to enter multiline text using the JavaFX TextArea. You can also use the JavaFX TextArea if your application requires multiline text.
How to use the JavaFX Text Field
The TextField in JavaFX is very easy to use. It is used to input something, and you can add the text field in your application if you have a login window and use it to enter the username and use the JavaFX PasswordField for your password as well or enter something from your application, like a license key, personal information and more.
TextField in JavaFX Examples
The following examples are perfect for beginners just starting to learn JavaFX. The examples below will teach how to create the text field, display it in the scene graph, setText() and getText() methods and show more interesting examples. Please proceed below to learn more about using the text field control.
Create the Text Field
The first step we are going to learn is to create the text field. You need to use the text field’s constructor to create the node, or you can use the Scene Builder if you are creating the application using the FXML-Based application. Creating the GUI using the scene builder is as easy as 1, 2, 3. You can simply drag and drop the components into the layout.
The example code below will show you how to create the Text Field using its constructor.
// Creating the text field with an empty text TextField textField = new TextField(); // creating the text field with an initial text TextField textField = new TextField("kensoftph.com");
Add the Text Field in JavaFX to the Scene Graph
Once you are done creating the text field. The next step you are going to do is to add or display it in the scene graph. You must know how to use the layout in JavaFX because we are also going to use the layout as it is the container of the child nodes like buttons, text fields, labels, etc. Please proceed below to learn more about how we can display the text field in the scene graph.
// create a layout StackPane layout = new StackPane(); //create the scene Scene scene = new Scene(layout, 400, 400); layout.getChildren().add(textField); stage.setScene(scene); stage.show();
Output
Get the entered text in the text field
The getText() method is one of the most used methods in the text field because, when you have a text field in your application you need to get the entered text from the user. The example code below will show you how to use the getText() method in JavaFX Text Field.
TextField textField = new TextField("kensoftph.com"); System.out.println(textField.getText());
Output
kensoftph.com
Set text in the text field
The setText() method is one of the most used methods in the text field because sometimes you also have to set a text in the text field if needed. The example code below will show you how to use the setText() method in JavaFX TextField.
TextField textField = new TextField(); textField.setText("kensoftph.com");
Output
Prompt in JavaFX TextField
Prompt text is a very useful feature to tell the user that they need to enter something like “Please enter your name”. If you don’t want to use a label to tell something to the user, you can use the prompt text feature instead. The example code below will show you how to set a prompt text.
TextField textField = new TextField(); textField.setPromptText("Enter your name");
Output
Cut, Copy, Paste, Undo, Redo
In this example, I will show you how to Cut, Copy, Paste, Undo, and Redo. This feature is very easy to make, and I will show you something different. We will use the Context Menu and Menu Items control in JavaFX. When you have implemented this feature in your application, you can simply right-click directly over from the TextField in JavaFX.
ContextMenu cm = new ContextMenu(); MenuItem item1 = new MenuItem("Cut"); MenuItem item2 = new MenuItem("Copy"); MenuItem item3 = new MenuItem("Paste"); MenuItem item4 = new MenuItem("Undo"); MenuItem item5 = new MenuItem("Redo"); cm.getItems().addAll(item1, item2, item3, item4, item5); TextField textField = new TextField(); textField.setContextMenu(cm); item1.setOnAction(event ->{ textField.cut(); }); item2.setOnAction(event ->{ textField.copy(); }); item3.setOnAction(event ->{ textField.paste(); }); item4.setOnAction(event ->{ textField.undo(); }); item5.setOnAction(event ->{ textField.redo(); });