×

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 Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 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 UI Controls

We can make an advanced graphical user interface (GUI) for the JavaFX application using UI Controls and layouts. Various UI elements can be used b the programmer in their application...

3 minutes read.

JavaFX Light Point

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

4 minutes read.

JavaFX Light Spot

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

4 minutes read.

JavaFX StackPane

In the JavaFX application, in order to set Stack Pane as a layout, the StackPane class is used. The StackPane layout allows us to arrange the components in the stack...

4 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

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 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 File Chooser

In the JavaFX application, in order to open or save the file, FileChooser is used. It allows user to choose the file from system location and save it in a...

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 Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

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

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

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 Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

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