How to create a password field using JavaFX?

JavaFXObject Oriented ProgrammingProgramming

The text field accepts and displays the text. In the latest versions of JavaFX, it accepts only a single line. In JavaFX the javafx.scene.control.TextField class represents the text field, this class inherits the javafx.scene.control.TextInputControl (base class of all the text controls) class. Using this you can accept input from the user and read it to your application.

Similar to text field a password field accepts text but instead of displaying the given text, it hides the typed characters by displaying an echo string.

In JavaFX the javafx.scene.control.PasswordField represents a password field and it inherits the Text class. To create a password field you need to instantiate this class.


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class PasswordFieldExample extends Application {
   public void start(Stage stage) {
      //Creating nodes
      TextField textField = new TextField();
      PasswordField pwdField = new PasswordField();
      //Creating labels
      Label label1 = new Label("Name: ");
      Label label2 = new Label("Pass word: ");
      //Adding labels for nodes
      HBox box = new HBox(5);
      box.setPadding(new Insets(25, 5 , 5, 50));
      box.getChildren().addAll(label1, textField, label2, pwdField);
      //Setting the stage
      Scene scene = new Scene(box, 595, 150, Color.BEIGE);
      stage.setTitle("Password Field Example");
   public static void main(String args[]){


Updated on 18-May-2020 06:23:23