×

JavaFX PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to enter input for specific fields. In JavaFX application, javafx.scene.control.PasswordField class is used to include Password Field. We can use the Password Field to create an instance of Password Field.

JavaFX PasswordField:

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;  


public class PasswordFieldUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
      Label rno=new Label(" Enter Roll No ");  
      Label pwd = new Label(" Enter Password");  
        TextField newtf=new TextField();  
        PasswordField newpf=new PasswordField();  
        newpf.setPromptText("Enter Password");  
        Button newbtn = new Button(" Submit ");  
        GridPane pane = new GridPane();  
        pane.addRow(0, rno, newtf);  
        pane.addRow(1, pwd, newpf);  
        pane.addRow(5, newbtn);  
        Scene scene=new Scene(pane,500,300);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle(" JavaFX Password Field Example ");  
        primaryStage.show();  
    }


    public static void main(String[] args) 
   {
        Application.launch(args);
    }
}

Output:

In order to create the Password Field 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.PasswordField, and javafx.scene.layout.GridPane.

Then we have created one class named PaaswordFieldUI 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 the button with PasswordField and Labels, a GirdPane object is created which is then passed to the Scene class object.

Label for Roll number and password is created using the constructor with label text passed to it. Then one TextField is created to get input of roll number and one Password field to get the password. The setPromptText() method is used to show text in the password field. 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 Password Field Example”. Also, it displays label to enter roll number and password. Then we can enter the same in the textfield and password field. Also, there is one submit button at last.

JavaFX PasswordField

Retrieving the data in the PasswordField:

In the JavaFX application, all the data entered in the PasswordField can be retrieved using the getText() method. This method returns a string that can be later printed or saved in the database.

Example:

import javafx.application.Application;  
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;  
import javafx.event.ActionEvent;  
import javafx.event.EventHandler;  
  
public class PasswordFieldUI extends Application {  
  
    @Override  
    public void start(Stage primaryStage) throws Exception {  
      Label rno=new Label(" Enter Roll No ");  
      Label pwd = new Label(" Enter Password ");  
        TextField newtf=new TextField();  
        PasswordField newpf=new PasswordField();  
        newpf.setPromptText("Enter Password");  
        Button newbtn = new Button("Submit");  
        newbtn.setOnAction(e->System.out.println("You entered: Roll NO : "+newtf.getText()+""+" Password: "+newpf.getText()));  
        GridPane pane = new GridPane();  
        pane.addRow(0, rno, newtf);  
        pane.addRow(1, pwd, newpf);  
        pane.addRow(5, newbtn);  
        Scene scene=new Scene(pane,500,300);  
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFX Password Field Example");  
        primaryStage.show();  
    }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Password Field 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.PasswordField, javafx.event.ActionEvent, javafx.event.EventHandler, and javafx.scene.layout.GridPane.

Then we have created one class named PaaswordFieldUI 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 the button with PasswordField and Labels, a GirdPane object is created which is then passed to the Scene class object.

In order to get Roll number and password, Label for Roll number and password is created. Constructor is used with text string passed in it. Two textfields one for roll number and one for taking password input is created. Submit button is created using constructor. Then using the setOnAction() method on the button, values of TextField and PasswordField is retrieved with the help of the getText() method and printed on the console.

Then using the addRow() method respective labels, passwordfield, 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 Password Field Example”. Also, it displays labels to enter roll number and password. Then we can enter the same in the textfield and password field. Also, there is one submit button at last. On clicking on the submit button it will display, You entered: Roll NO : 101 Password: India@123.

JavaFX PasswordField

Related Topics

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 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 ProgressBar

In the JavaFX application, in order to show the progress of work ProgressBar is used. It shows how much work is done. In order to use ProgressBar in JavaFX, javafx.scene.control.ProgressBar...

4 minutes read.

JavaFX PasswordField

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

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

In the JavaFX application, in order to set HBox as a layout, the HBox class is used. The HBox layout allows us to arrange the components in one row. All...

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

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 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 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 Bar Chart

In the JavaFX application, in order to create Bar Chart, the BarChart class is used. The Bar Chart allows us to plot data along different bars to show the scales...

4 minutes read.

JavaFX Color Input

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

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 Tutorial

Introduction of JavaFX: The most popular applications called Rich Internet Application are build using the JavaFX. This application can run across many platforms. Various devices such as mobile, computers, and tablets...

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 Gradient 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 gradient colors in these shapes using the...

7 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Path Transition

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

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 Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.