Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJavaFx | PasswordField

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.




    // 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);
        }
    }

    
    

    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.




    // 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);
        }
    }

    
    

    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

RELATED ARTICLES

Most Popular

Recent Comments