Introduction of JPasswordField


    Naming Convention
    SDJ PasswordField

    JPasswordField (a direct subclass of JTextField)

    you can suppress the display of input. Each character entered can be replaced by an echo character. This allows confidential input for passwords, for example. By default, the echo character is the asterisk, *.

JPasswordField Constructor


    Constructs a new JPasswordField, with a default document, null starting text string, and 0 column width.

    JPasswordField(Document doc, String txt, int columns)

    Constructs a new JPasswordField that uses the given text storage model and the given number of columns

    JPasswordField(int columns)

    Constructs a new empty JPasswordField with the specified number of columns.

    JPasswordField(String text)

    Constructs a new JPasswordField initialized with the specified text..

    JPasswordField(String text, int columns)

    Constructs a new empty JPasswordField with the specified number of columns.

Example:JPasswordField Constructor

                package com.swing.sdj;
                import java.awt.BorderLayout;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import java.awt.event.WindowAdapter;
                import java.awt.event.WindowEvent;
                import javax.swing.BorderFactory;
                import javax.swing.JFrame;
                import javax.swing.JLabel;
                import javax.swing.JOptionPane;
                import javax.swing.JPanel;
                import javax.swing.JPasswordField;
                public class JPasswordDemo
                    public static void main(String[] argv)
                        final JFrame frame = new JFrame("SDJ JPassword Usage Demo");
                        JLabel jlbPassword = new JLabel("Enter the password: ");
                        JPasswordField jpwName = new JPasswordField(10);
                        jpwName.addActionListener(new ActionListener() {
                            public void actionPerformed(ActionEvent e)
                                JPasswordField input = (JPasswordField) e.getSource();
                                char[] password = input.getPassword();
                                if (isPasswordCorrect(password))
                                    JOptionPane.showMessageDialog(frame,"Correct  password.");
                                    JOptionPane.showMessageDialog(frame,"Sorry. Try again.", "Error Message",JOptionPane.ERROR_MESSAGE);
                        JPanel jplContentPane = new JPanel(new BorderLayout());
                        jplContentPane.setBorder(BorderFactory.createEmptyBorder(20, 20,20, 20));
                        jplContentPane.add(jlbPassword, BorderLayout.WEST);
                        jplContentPane.add(jpwName, BorderLayout.CENTER);
                        frame.addWindowListener(new WindowAdapter() {
                            public void windowClosing(WindowEvent e) {
                    private static boolean isPasswordCorrect(char[] inputPassword)
                        char[] actualPassword = { 's  ', 'd ', ' j ', 'i ', 'n', 'f', 'o','s','o','f','t'};
                        if (inputPassword.length != actualPassword.length)
                            return false; /* Return false if lengths are unequal */
                        for (int i = 0; i < inputPassword.length; i++)
                            if (inputPassword[i] != actualPassword[i]) {
                                return false;
                            } else {
                                return true;


Naming Convention
SDJ Wrong Password Demo

    Enter the correct password

Naming Convention
SDJ correct Password Demo