Search code examples
javapasswordspassword-strength

React by each character input


How to inform a user who is filling in the password field at any moment what the strength of his password is like Email program which inform at each moment when you filling the password field I want this feature in swing

I can't find any solution for this problem on Internet


Solution

  • I created a simple GUI to illustrate the process. I placed a DocumentListener on the Document of the JPasswordField. Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

    Example

    The password checker checks the length of the password. You would change this to test for whatever you consider to be a good or excellent password.

    Here's the complete runnable code.

    import java.awt.BorderLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.SwingUtilities;
    import javax.swing.event.DocumentEvent;
    import javax.swing.event.DocumentListener;
    import javax.swing.text.Document;
    
    public class JPasswordFieldExample implements Runnable {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new JPasswordFieldExample());
        }
    
        private JLabel strengthLabel;
    
        @Override
        public void run() {
            JFrame frame = new JFrame("JPasswordField Example");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(createPasswordPanel(), BorderLayout.CENTER);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JPanel createPasswordPanel() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    
            JPasswordField passwordField = new JPasswordField(30);
            Document document = passwordField.getDocument();
            document.addDocumentListener(new DocumentListener() {
    
                @Override
                public void insertUpdate(DocumentEvent event) {
                    checkPassword(event);
                }
    
                @Override
                public void removeUpdate(DocumentEvent event) {
                    checkPassword(event);
                }
    
                @Override
                public void changedUpdate(DocumentEvent event) {
                    checkPassword(event);
                }
    
                private void checkPassword(DocumentEvent event) {
                    Document document = event.getDocument();
                    if (document.getLength() < 8) {
                        strengthLabel.setText("Poor");
                    } else if (document.getLength() < 12) {
                        strengthLabel.setText("Good");
                    } else {
                        strengthLabel.setText("Excellent");
                    }
                }
    
            });
            panel.add(passwordField, BorderLayout.CENTER);
    
            strengthLabel = new JLabel(" ");
            panel.add(strengthLabel, BorderLayout.SOUTH);
    
            return panel;
        }
    
    }