Creating Password Field by Using JPasswordField Class

In this tutorial, we are going to show you how to create password field using JPasswordField class.

The password field allows user to enter password. For security reason, password field displays echo characters instead of the password itself. The default echo character is (*).

In Java swing, to create a password field you use JPasswordField class. You can assign a different echo character other than the default one (*) using setEchoChar() method. You can get the password using getPassword() method. If you use copy() orcut() method, you will get nothing but beep sound.

The following table illustrates some common uses constructors of the JPasswordField class:

JPasswordField ConstructorsMeaning
public JPasswordField ()Creates a new password field.
public  JPasswordField (Document doc, String text, int columns)Creates a new password field with given document and number of columns.
public  JPasswordField(String text)Creates a new password field with a given text.
public  JPasswordField(int columns)Creates a new password field with a given columns.
public  JPasswordField(String text, int columns)Creates a new password field with given text and number of columns.

Example of using JPasswordField class

In this example, we will use JPasswordField class to create a simple password field.

JPasswordField Demo
package jpasswordfielddemo;

import java.awt.event.*;
import javax.swing.*;

public class Main {

    public static void main(String[] args) {
        final JFrame frame = new JFrame("JPasswordField Demo");

        JLabel lblUser = new JLabel("User Name:");
        JTextField tfUser = new JTextField(20);
        lblUser.setLabelFor(tfUser);

        JLabel lblPassword = new JLabel("Password:");
        final JPasswordField pfPassword = new JPasswordField(20);
        lblPassword.setLabelFor(pfPassword);

        JButton btnGet = new JButton("Display Password");
        btnGet.addActionListener(
                new ActionListener() {

                    public void actionPerformed(ActionEvent e) {
                        String password = new String(pfPassword.getPassword());
                        JOptionPane.showMessageDialog(frame,
                                "Password is " + password);
                    }
                });

        JButton btnLogin = new JButton("Login");

        JPanel panel = new JPanel();
        panel.setLayout(new SpringLayout());

        panel.add(lblUser);
        panel.add(tfUser);
        panel.add(lblPassword);
        panel.add(pfPassword);
        panel.add(btnLogin);
        panel.add(btnGet);

        SpringUtilities.makeCompactGrid(panel,
                3, 2, //rows, cols
                6, 6, //initX, initY
                6, 6); //xPad, yPad

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(300, 120);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }
}Code language: JavaScript (javascript)

In order to run the demo application, you’ll need SpringUtilities class. Click on the download link below to download SpringUtilities.java file.

Java Swing Spring Utilities (5702 downloads )