JavaFX PasswordField

In JavaFX, PasswordField is same as that of TextField, except that the characters entered in it are masked.

In this tutorial, we will learn how to define a Password field in our GUI application using Java, and how to read the password provided by the user in the PasswordField, when an event occurs like a button click.

Example 1 – JavaFX PasswordField Basic Example

This is a simple example to demonstrate how to define a PasswordField and show it in GUI.

JavaFxPasswordFieldTutorial.java

import javafx.application.Application;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
 
public class JavaFxPasswordFieldTutorial extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            // set title
            primaryStage.setTitle("Password Field Tutorial - tutorialkart.com");
            
            //javafx password field
            PasswordField passwordField = new PasswordField();
          
            // tile pane
            TilePane tilePane = new TilePane();
            
            // add password field to the tile pane
            tilePane.getChildren().add(passwordField);
            
            //set up scene
            Scene scene = new Scene(tilePane, 400, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Run this Java program, and you should see a PasswordField as shown below. You may start typing some characters into the password field, and you should see that they are masked.

JavaFX PasswordField Tutorial

Example 2 – JavaFX PasswordField – Get the Value

You can get the value entered in PasswordField control using getText() method. In this example, we shall print the password entered in the field to the console, when user clicks a button.

JavaFxPasswordFieldTutorial2.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.layout.TilePane;
import javafx.stage.Stage;
 
public class JavaFxPasswordFieldTutorial2 extends Application {
    
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        try {
            // set title
            primaryStage.setTitle("Password Field Tutorial - tutorialkart.com");
            
            //javafx password field
            PasswordField passwordField = new PasswordField();
          
            Button button = new Button("Submit");
            button.setOnAction(action -> {
                System.out.println("Password entered: "+passwordField.getText());
            });
            
            // tile pane
            TilePane tilePane = new TilePane();
            
            // add password field to the tile pane
            tilePane.getChildren().add(passwordField);
            tilePane.getChildren().add(button);
            
            //set up scene
            Scene scene = new Scene(tilePane, 400, 100);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

Run the application. A window appears with Password field and button. Enter some text in the PasswordField and click on Submit button.

JavaFX PasswordField - Get the Value

When you click on the Submit button, we shall get the value entered in the password field and print it in the console.

JavaFX PasswordField - Get the Value - Console output

Conclusion

In this JavaFX Tutorial, we learned how to define a PasswordField control in GUI, read the text entered in password filed.