JavaFx | PasswordField

PasswordField class is a part of JavaFX package. It is a Text field that masks entered characters (the characters that are entered are not shown to the user). It allows the user to enter a single-line of unformatted text, hence it does not allow multi-line input.
Constructor of the PasswordField class :
- PasswordField(): creates a new PasswordField
(PasswordField inherits TextField so all the methods of TextField can be used here. There are no separate methods for the password field, all are inherited from the text field.)
Below programs illustrate the use of PasswordField class:
- Java program to create a Password field: This program creates a PasswordField indicated by the name b. The PasswordField will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a Title-pane is created, on which addChildren() method is called to attach the PasswordField inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.
// Java program to create a passwordfieldimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;publicclassPasswordfieldextendsApplication{// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating Passwordfield");// create a PasswordfieldPasswordField b =newPasswordField();// create a tile paneTilePane r =newTilePane();// add password fieldr.getChildren().add(b);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){// launch the applicationlaunch(args);}}Output:
- Java program to create a passwordfield and add a event handler: This program creates a PasswordField indicated by the name b. We will create a label which will display the password when the enter key is pressed. We will create an event handler that will handle the event of the password field and the event handler would be added to the passwordfield using setOnAction() method. The PasswordField will be created inside a scene, which in turn will be hosted inside a stage (which is the top level JavaFX container). The function setTitle() is used to provide title to the stage. Then a Title-pane is created, on which addChildren() method is called to attach the PasswordField and a label inside the scene, along with the resolution specified by (200, 200) in the code. Finally, the show() method is called to display the final results.
// Java program to create a passwordfield and add// a event handler to handle the event of Passwordfieldimportjavafx.application.Application;importjavafx.scene.Scene;importjavafx.scene.control.*;importjavafx.scene.layout.*;importjavafx.event.ActionEvent;importjavafx.event.EventHandler;importjavafx.scene.control.Label;importjavafx.stage.Stage;publicclassPasswordfield_1extendsApplication{// launch the applicationpublicvoidstart(Stage s){// set title for the stages.setTitle("creating Passwordfield");// create a PasswordfieldPasswordField b =newPasswordField();// create a tile paneTilePane r =newTilePane();// create a labelLabel l =newLabel("no Password");// action eventEventHandler<ActionEvent> event =newEventHandler<ActionEvent>(){publicvoidhandle(ActionEvent e){l.setText(b.getText());}};// when enter is pressedb.setOnAction(event);// add password fieldr.getChildren().add(b);r.getChildren().add(l);// create a sceneScene sc =newScene(r,200,200);// set the scenes.setScene(sc);s.show();}publicstaticvoidmain(String args[]){//launch the applicationlaunch(args);}}Output:
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/PasswordField.html




