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 :

  1. 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:

  1. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a passwordfield
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.control.Label;
    import javafx.stage.Stage;
    public class Passwordfield extends Application
    {
          
        // launch the application
        public void start(Stage s)
        {
            // set title for the stage
            s.setTitle("creating Passwordfield");
              
            // create a Passwordfield
            PasswordField b =new PasswordField();
              
            // create a tile pane
            TilePane r = new TilePane();
              
              
            // add password field
            r.getChildren().add(b);
              
            // create a scene
            Scene sc =new Scene(r,200,200);
              
            // set the scene
            s.setScene(sc);
             
            s.show();
      
        }
          
        public static void main(String args[])
        {
            // launch the application
           launch(args);
        }
    }

    chevron_right

    
    

    Output:

  2. 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.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java program to create a passwordfield and add
    // a event handler to handle the event of Passwordfield
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.control.Label;
    import javafx.stage.Stage;
    public class Passwordfield_1 extends Application
    {
          
        // launch the application
        public void start(Stage s)
        {
            // set title for the stage
            s.setTitle("creating Passwordfield");
              
            // create a Passwordfield
            PasswordField b =new PasswordField();
              
            // create a tile pane
            TilePane r = new TilePane();
              
            // create a label
            Label l = new Label("no Password");
              
            // action event
            EventHandler<ActionEvent> event = new EventHandler<ActionEvent>(){
            public void handle(ActionEvent e)
            {
                l.setText(b.getText());
            }
            };
              
            // when enter is pressed
            b.setOnAction(event);
              
            // add password field
            r.getChildren().add(b);
            r.getChildren().add(l);
              
            // create a scene
            Scene sc =new Scene(r,200,200);
              
            // set the scene
            s.setScene(sc);
             
            s.show();
      
        }
          
        public static void main(String args[])
        {
            //launch the application
           launch(args);
        }
    }

    chevron_right

    
    

    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



My Personal Notes arrow_drop_up

Second year Department of Information Technology Jadavpur University

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.