×

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 Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX Installation

In order to run JavaFX applications, we need to have eclipse and java installed on our machine. Prerequisite: Operating systemAny Operating system (Windows, Linux, Mac)Memory requirementNo minimum requirementDisk SpaceNo minimum requirementJDK versionJDK...

3 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 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 TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 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 Cylinder

In the JavaFX application, in order to draw a Cylinder, the Cylinder class is used. The Cylinder allows us to form a 3D shape having rectangular faces and a straight...

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 Parallel Transition

In the JavaFX application, in order to generate the parallel transition, we have to apply parallel transition on any particular shape. It has the ability to execute multiple transactions parallelly....

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

JavaFX ID selector

In the JavaFX application, in order to apply various css effect using the id selector, we use the various css classes available in the JavaFX. This is generally used to...

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

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 Light Distant

In the JavaFX application, in order to apply various Light Distant effect from a single source, we use this class. It has the ability to apply light from a distant...

4 minutes read.

JavaFX Motion Blur

In the JavaFX application, in order to apply various Motion blur effect, we use Motion Blur class. It has the ability to apply the Motion blur effect on the given...

4 minutes read.

JavaFX Pause Transition

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

4 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 Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

4 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 Rotate Transition

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

4 minutes read.