JavaFX Password Field

0

JavaFX Password Field tutorial:

Password Field is an input field like TextField but it is used for taking password as input.
To secure password it hide the characters entered by user. It hide password by display an echo string.

Constructors:

PasswordField()
Creates a default PasswordField instance.

Example of password field:


package javafxtuts;

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.layout.VBox;
import javafx.stage.Stage;
/**
 *
 * @author javafxtuts
 */
public class Javafxtuts extends Application {

  @Override
  public void start(Stage primaryStage) {
      
      // setting title of application
      primaryStage.setTitle("javafxtuts.com");
      //creating HBox layout
      HBox hb1 = new HBox();
      HBox hb2 = new HBox();
	//creating VBox layout
      VBox root = new VBox();
      Label lb =new Label("User Name : ");
      Label lb1 =new Label(" Password : ");
      //to set space for label from other nodes of VBox
      lb.setPadding(new Insets(10));
      lb1.setPadding(new Insets(10));
      //to Create a text field
      TextField tf =new TextField();
      tf.setText("Write UserName ");
      
      PasswordField pf =new PasswordField();
      
      // to set space for hb layout
      hb1.setPadding(new Insets(10));
      hb2.setPadding(new Insets(10));
      
      //add all children of Vbox layout(root)
      hb1.getChildren().addAll(lb,tf);
      hb2.getChildren().addAll(lb1,pf);
      root.getChildren().addAll(hb1,hb2);
      Scene scene = new Scene(root, 350, 175);
      primaryStage.setScene(scene);
      primaryStage.show();
  }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

Output :

Explanation of Password Field:

  • First creating layout HBox for user name other HBox for password and put both HBox in VBox Layout.AS
HBox hb1 = new HBox();
HBox hb2 = new HBox();
VBox vb = new VBox();

PasswordField pf =new PasswordField();

  • To create password field
  • Share.

    Leave A Reply