PasswordField is a part of javax.swing package . The class JPasswordField is a component that allows editing of a single line of text where the view indicates that something was typed by does not show the actual characters. JPasswordField inherits the JTextField class in javax.swing package.
Constructors of the class are :
- JPasswordField(): constructor that creates a new PasswordField
- JPasswordField(int columns) : constructor that creates a new empty PasswordField with specified number of columns.
- JPasswordField(String Password) : constructor that creates a new empty Password field initialized with the given string.
- JPasswordField(String Password, int columns) : constructor that creates a new empty PasswordField with the given string and a specified number of columns .
- JPasswordField(Document doc, String Password, int columns) : constructor that creates a Passwordfield that uses the given text storage model and the given number of columns.
Commonly used method of JPasswordField :
- char getEchoChar() : returns the character used for echoing in JPasswordField.
- setEchoChar(char c) : set the echo character for JPasswordField.
- String getPassword() : returns the text contained in JPasswordField.
- String getText() : returns the text contained in JPasswordField.
1. Program to enter name and password using JTextField and JPasswordField
2. Program to change the echo character of JPasswordField and set initial text for password field
Note : The above programs might not run in an online compiler use an offline IDE.
the default text and number of columns of password can be changed by the programmer as per their need.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.