×

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields. In JavaFX application, javafx.scene.control.TextField class is used to include TextField. We can use the TextField by creating the instance of TextField class also various methods are available.

JavaFX TextField:

Example:

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;  
  
public class TextFieldUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
    Label lbl=new Label(" Enter the student information ");  


    Label Fname=new Label(" First Name ");  
        Label lname = new Label(" Last Name ");  
        TextField newtf1=new TextField();  
        TextField newtf2=new TextField();  
        Button newbtn = new Button("Submit");  
        GridPane root = new GridPane();  
        root.addRow(0, lbl);  
        root.addRow(1, Fname, newtf1);  
        root.addRow(2, lname, newtf2);  
        root.addRow(3, newbtn);  
        Scene scene=new Scene(root,600,400);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle(" JavaFX Text Field Example");  
        primaryStage.show();  
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the TextField in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene,  javafx.scene.control.Button, javafx.stage.Stage, javafx.scene.control.TextField, and javafx.scene.layout.GridPane.

Then we have created one class named TextFieldUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a button with TextField and Labels, a GirdPane object is created which is then passed to the Scene class object.

Label for firstname and lastname is created using the constructor with label text passed to it. Then two TextField are created to get input of firstname and lastname from the user. Submit button is created using the button constructor. Then using the addRow() method respective labels and textfield are added in a row. This GridPane object is given to a scene class constructor with specified width and height. The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "JavaFX Text Field Example”. Then the label is displayed saying Enter student information. For getting the input from the user labels with textfield are created with one submit button at the end.

JavaFX TextField

Retrieving the data in the TextField:

In the JavaFX application, all the data entered in the TextField can be retrieved using the getText() method. This method returns a string that can be later printed or saved in the database. It will help to get the data entered by the user in the form.

Example:

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;  
import javafx.event.ActionEvent;  
import javafx.event.EventHandler;  
  
public class TextFieldUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
    Label firstname = new Label(" First Name");  
        Label lastname = new Label(" Last Name");  
        TextField newtf1=new TextField();  
        TextField newtf2=new TextField();  
        Button b1 = new Button(" Submit");  
        b1.setOnAction(e->System.out.println("You entered: First Name: "+newtf1.getText()+""+"Last Name: "+newtf2.getText()));  
        GridPane root = new GridPane();  
        root.addRow(0, firstname, newtf1);  
        root.addRow(1, lastname, newtf2);  
        root.addRow(2, b1);  
        Scene scene=new Scene(root,500,300);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("Text Field Example");  
        primaryStage.show();  
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the TextField and Action on it in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene,  javafx.scene.control.Button, javafx.stage.Stage, javafx.scene.control.TextField, javafx.scene.layout.GridPane, javafx.event.ActionEvent, and javafx.event.EventHandler.

Then we have created one class named TextFieldUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a button with TextField and Labels, a GirdPane object is created which is then passed to the Scene class object.

Label for firstname and lastname is created using the constructor with label text passed to it. Then two TextField are created to get input of firstname and lastname from the user. Submit button is created using the button constructor. The action is added on the button using the setOnAction() method which will use the getText() method to retrieve the value entered in the TextField.

Then using the addRow() method respective labels and textfield are added in a row. This GridPane object is given to a scene class constructor with specified width and height. The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, "Text Field Example”. The user enters first name and last name in the TextField and when the user clicks on the submit button, it will show, You entered: First Name: Sanket Last Name: Chaudhari.

JavaFX TextField

Related Topics

JavaFX Image Input

In the JavaFX application, in order to apply various image input effect, we use the ImageInput class. It has the ability to fill the specified shape with a given image...

4 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Button Styling

 In the JavaFX application, we can apply CSS styling to buttons in various ways. We can change the size of the button as well as border colour, text colour can...

3 minutes read.

JavaFX Scale Transition

In the JavaFX application, in order to generate the scale transition, we have to apply scale transition on any particular shape. It has the ability to scale the shape along...

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Box

In the JavaFX application, in order to draw a Box, the Box class is used. The Box allows us to form 3D shape having rectangular faces. All methods needed for...

4 minutes read.

JavaFX Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.

JavaFX RadioButton

In the JavaFX application, to get the user information in the form of choice from the user, a form is created using radio buttons that allow the user to select...

3 minutes read.

JavaFX Convenience Methods

The JavaFX allows us to use various convenience methods to handle events on various nodes in JavaFX applications. The convenience methods provides us easy and effective way to add any...

4 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Sphere

In the JavaFX application, in order to draw a Sphere, the Sphere class is used. The Sphere allows us to form a 3D shape having circular faces. All methods needed...

4 minutes read.

JavaFX Circle

In the JavaFX application, in order to draw a circle, the Circle class is used. The Circle allows us to join any points in the space. All methods needed for...

4 minutes read.

JavaFX Scaling

In the JavaFX application, in order to apply various Scaling effects, we use this class. It has the ability to change the size of the given object. All methods needed...

5 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX Color

In the JavaFX application, in order to draw any shape, we are using classes present in the javafx.scene.shape package. We can fill the colors in these shapes using the setFill()...

6 minutes read.

JavaFX Translate Transition

In the JavaFX application, in order to generate the translate transition, we have to apply translate transition on any particular shape. It has the ability to translate the shape along...

3 minutes read.