Updated March 17, 2023
Introduction to JavaFX TextField
In the JavaFX package, a class known as TextField helps the users to enter the unformatted text that can be read by the application. The text should not be of multiple lines as it allows only a single line of the input text. This JavaFX TextField control can be instantiated from the class javafx.scene.control.TextField. The constructors, methods and example of JavaFX TextField will be discussed in the following sections.
JavaFX TextField Constructors
There are two constructors for JavaFX TextField.
1. TextField(): A TextField will be created with empty text content.
Code:
// create a TextField
TextField tf = new TextField();
2. TextField(Strings): A TextField will be created with a text s as its label.
Code:
// create a TextField
TextField tf = new TextField("Be Happy Always");
Methods
Following are some of the commonly used methods in JavaFX TextField:
- setPrefColumnCount(int v): PrefColumnCount property’s value will be set.
- setOnAction(EventHandler v): OnAction property’s value will be set.
- setAlignment(Pos p): Alignment property’s value will be set.
- getAlignment(): Alignment property’s value will be returned.
- getPrefColumnCount(): PrefColumnCount property’s value will be returned.
- getOnAction(): OnAction property’s value will be returned.
- getCharacters(): Character sequence will be returned back the content of the text field.
- prefColumnCountProperty(): Count of text columns preferred.
- OnActionProperty(): If the OnAction handler has been assigned with a text field, null will be returned. If it is present, that particular action handler will be returned.
How to Create a JavaFX TextField?
In order to create a textfield, the following steps have to be performed:
Step #1 – Set the Title for the Stage Created
JavaFX represents the content displayed inside a window/inside a Stage.
Code:
s.setTitle("JavaFX TextField Sample");
Step #2 – Create a TextField
Since both parameterized and non-parameterized constructors can be used, any one of the following syntaxes can be considered based on the requirement.
Code:
//parameterized constructor
TextField tf = new TextField(“Be Happy Always”);
//non-paramterized constructor
TextField tf = new TextField();
Step #3 – Add the TextField created to the Scene Graph
After the creation of the textfield, create a scene and add the TextField to the scene graph using the below steps.
Code:
Scene sc = new Scene(hb, 300, 200);
s.setScene(sc);
s.show();
Program to Implement JavaFX TextField
Following are some of the programs that will help to understand JavaFX TextField:
Example #1
Java program to Demonstrate One Text Field.
Code:
//Java program to demonstrate one text field
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
//child class that extends Application base class
public class JavaFXTextFieldExample extends Application {
//application launches here
@Override
public void start(Stage s) throws Exception {
//set the title for the stage
s.setTitle("JavaFX Textfield example");
//create a textfield
TextField tf = new TextField();
//create an hbox
HBox hb = new HBox(tf);
//create a scene
Scene scene = new Scene(hb, 300, 200);
//set the scene
s.setScene(scene);
//display the result
s.show();
}
//main method
public static void main(String[] args) {
Application.launch(args);
}
}
Output 1: Here the text can be typed in the blank bracket.
Output 2: Here, text can be typed as shown in the below screenshot.
Explanation to the above code: First, a textfield is created with the help of a non-parameterized constructor. Added the created textfield into the scene graph. At last, a textfield is displayed as output where text can be inserted.
Example #2
Java program to Demonstrate One Text Field with a Button and Returns the Text Entered.
Code:
//Java program to implement JavaFX TextField with a button and returns the text entered
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
//child class that extends Application class
public class JavaFXTextFieldExample extends Application {
//main method
public static void main(String[] args) {
//launches the application
launch(args);
}
//application starts here
@Override
public void start(Stage s) throws Exception
{
//create a label name
Label name=new Label("Full name : ");
//create a label age
Label Age = new Label("Age : ");
// create two textfields for label name and label age
TextField t1=new TextField();
TextField t2=new TextField();
//create a button
Button btn = new Button("Click me");
//action to be performed
btn.setOnAction(e->System.out.println("The name you entered : "+ t1.getText()
+ "\nThe age you entered: "+t2.getText()));
//create a gridpane
GridPane r = new GridPane();
r.addRow(0, name, t1);
r.addRow(1, Age, t2);
r.addRow(2, btn);
//create scene
Scene sc=new Scene(r,400,300);
//set the scene
s.setScene(sc);
//set the title
s.setTitle("Text Field Example");
//display the result
s.show();
}
}
Output 1: Enter the required TextField Example.
Output 2: Once the above dialog appears, enter the text that you want to submit.
Output 3: After you submit the button, the text you entered will be displayed in the console as shown below.
Explanation to the above code: First, two TextFields and 1 button are created. Once the button is clicked, an action to display the entered text in the console will be triggered.
Example #3
Java program to create a text field with an initial text and set an event handler once any action occurs in the textfield.
Code:
//Java program to create a text field with an initial text and set an event handler once any action occurs in the textfield
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.stage.Stage;
import javafx.geometry.*;
public class JavaFXTextFieldExample extends Application {
//application starts here
public void start(Stage s)
{
// set title
s.setTitle("JavaFX TxtField example");
// create textfield
TextField t = new TextField("type here");
// set alignment
t.setAlignment(Pos.CENTER);
// tile pane
TilePane tp = new TilePane();
// label
Label l = new Label("Ypu have enetered nothing");
// action event
EventHandler<ActionEvent> ev = new EventHandler<ActionEvent>() {
//action that to be performed
public void handle(ActionEvent e)
{
l.setText(t.getText());
}
};
t.setOnAction(ev);
tp.getChildren().add(t);
tp.getChildren().add(l);
Scene sc = new Scene(tp, 200, 200);
s.setScene(sc);
s.show();
}
public static void main(String args[])
{
// launch the application
launch(args);
}
}
Output 1: When u type here, you have entered nothing.
Output 2: When a text is entered, the label will change as depicted below.
Explanation to the above code: Create a text field with an initial text. Set an event to be performed when the text is entered. The label will be changed corresponding to the entered text.
Conclusion
In JavaFX, TextFields are used to get the text input from the user so that it can be read by the application. It uses both parameterized and non-parameterized constructors based on the user’s need.
Recommended Articles
This is a guide to JavaFX TextField. Here we discuss two constructors, methods, how to create and program to implement in JavaFX TextField. You can also go through our other related articles to learn more