Updated March 18, 2023
Introduction to JPasswordField
JPasswordField is a Swing component and an input field which facilitates the user to enter their password. You must have seen such a field while logging in to any website like Facebook or Gmail which shows “*” or any other character when you type your password into the text box provided. JPasswordField is the same thing.
Declaration of Jpasswordfield:
public class JPasswordField extends JTextField
As you can see, this class is a subclass of JTextField, it inherits all the properties of a text field plus its own functionalities like masking the entered characters.
Constructor Details
- JPasswordField(): This is the simplest constructor that creates a password field with default document, zero column width and no (null) starting text string.
- JPasswordField(Document doc, String txt, int columns): This constructor creates a password field with a specified document, specified column width, and specified default password.
- JPasswordField(int columns): This constructor creates a password field with specified column width.
- JPasswordField(String text): This constructor creates a password field with the specified default password.
- JPasswordField(String text, int columns): This constructor creates a password field with specified column width and specified default password.
Method details
- char[] getPassword(): This is an important and most useful method of JPasswordField class which returns the password, as a character array, entered in this JPasswordField.
- String getText(): This method returns the password, as a string, entered in this JPasswordField. But this method is deprecated in Java 2 platform v1.2, replaced by the getPassword() method.
- String getText(int offs, int len): This method returns a portion of the password, as a string, entered in this JPasswordField. But this method is deprecated in Java 2 platform v1.2, replaced by getPassword() method.
- void copy(): This method invokes provideErrorFeedback on the current look and feel which initiates an error beep.
- void cut(): This method invokes provideErrorFeedback on the current look and feels which initiates an error beep.
- boolean echoCharIsSet(): This method returns true if a character is set for echoing to this JPasswordField. Otherwise false.
- char getEchoChar(): This method returns the character which is set to this JPasswordField for echoing.
- void setEchoChar(char c): This method sets the echo character to this password field.
- String getUIClassID(): This method returns the name of the look and feel class that renders this component.
- protected String paramString(): This method returns a string representation of this password field.
- void updateUI(): This method reloads the pluggable UI of this password field.
- AccessibleContext getAccessibleContext(): This method returns the AccessibleContext associated with this JPasswordField.
Example of Jpasswordfield
Below is the example for Jpasswordfield:
Code:
package application;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
public class JPasswordFieldDemo {
public static void main(String[] args) {
JFrame app = new JFrame("JPasswordField Demo");
app.setLayout(new GridLayout(4, 0));
app.setSize(400, 400);
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();
JPanel panel4 = new JPanel();
JLabel label = new JLabel("Password : ");
JPasswordField passwordField = new JPasswordField(10);
JButton submit = new JButton("submit");
panel1.add(label);
panel1.add(passwordField);
panel1.add(submit);
JLabel status = new JLabel("Password is : ");
panel2.add(status);
JLabel label2 = new JLabel("Set echo character : ");
JTextField textFieldForEchoChar = new JTextField(5);
JButton setEchoCharButton = new JButton("SetEchoChar");
JLabel statusOfSetEchoChar = new JLabel("Echo character is : " + passwordField.getEchoChar());
panel3.add(label2);
panel3.add(textFieldForEchoChar);
panel3.add(setEchoCharButton);
panel4.add(statusOfSetEchoChar);
app.add(panel1);
app.add(panel2);
app.add(panel3);
app.add(panel4);
app.setVisible(true);
ActionListener actionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();if (command.equals("submit")) {
String password = new String(passwordField.getPassword());
status.setText("Password is : " + password);
}
if (command.equals("SetEchoChar")) {
char echoChar = textFieldForEchoChar.getText().charAt(0);
passwordField.setEchoChar(echoChar);
statusOfSetEchoChar.setText("Echo character set to: " + echoChar);
}
}
};
submit.addActionListener(actionListener);
setEchoCharButton.addActionListener(actionListener);
}
}
Explanation:
This is a simple application in which JPasswordField and its functionalities are shown.
- At the top, there is a JFrame which is our application.
- In this JFrame four panels are added.
- In the first panel, a JPasswordField is added so that the user can enter the password here.
- And a button is added to submit the password.
- In the second panel, a JLabel is added to show the password which the user has entered. When the user clicks on the submit button from the first panel, this status will be changed, and the latest password will be shown.
- Status in the second panel is updated using an action listener which is added to the submit button.
- When the user types the password, it is not shown in the JPasswordField, instead, echo chars are shown, and the password is hidden.
- There is a default echo char. Also, we can set the echo char for the password field. Now we are going to do that.
- In the third panel, a text box and a button are added. This text box is to take echo41char from the user.
- Users will have to enter a character and press the button and echo char will be set to the password field.
- In the fourth panel, the user can see the current echo char or newly set echo char of the password field. This is achieved using a JLabel and an action listener added to the button in the third panel.
Output:
1. This is the application in which the user can enter the password, see the entered password, change the echo char of the password field and see the current echo char:
2. Now user enters the password (“12345”) and clicks on the submit button. Then, the password entered by the user is shown in the status below the password field. Also, you can see the current (default) echo char of the password field at the bottom:
3. Now the user wants to change the echo character of the password field. To achieve this, the user enters “*” in the second text box and presses the “SetEchoChar” button and the echo char is changed in the password field automatically.
4. Similarly, the user sets the echo char to “#”:
Conclusion – Jpasswordfield
JPasswordField is an important component in Swing which enables the user to enter the password without visually disposing it to others. This file can be used to develop a login and sign up page of any application. This increases the security of the application and makes them look and feel better. Also, the developer can set his favorite echo char!
Recommended Articles
This is a guide to Jpasswordfield. Here we discuss the Jpasswordfield can be used to develop a log in and sign up page of any application. You may also look at the following article to learn more –