Explore Java Hacks and Tutorials
Be Social. Be Updated.

Creating Password Checker in Java


Java Cup Logo - java-demos.blogspot.com
The following example illustrates creating a password strength checker and matcher which goes on the basis of char count and also text in the type and re-type password fields. This is just a demo and is not a great fit for real time use.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
class PasswordStrengthChecker extends JFrame
{
JPasswordField t,rt;
JLabel lt,lrt,s;
JProgressBar jp;
JPanel panel;
String pw1,pw2;

public PasswordStrengthChecker()
{
setTitle("Password Strength");
setLayout(new GridBagLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);

panel=new JPanel();
panel.setLayout(new GridLayout(3,2));
add(panel);

lt=new JLabel("Password");
t=new JPasswordField(20);

lrt=new JLabel("Re-Type Password");
rt=new JPasswordField(20);

s=new JLabel("Strength(by count)");

jp=new JProgressBar();
jp.setForeground(Color.blue);
jp.setMaximum(10);

panel.add(lt);
panel.add(t);

panel.add(lrt);
panel.add(rt);

panel.add(s);
panel.add(jp);

pw1="";
pw2="";

t.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent ce)
{
pw1=new String(t.getPassword());
jp.setValue(pw1.length());

if(!pw1.isEmpty())
{
if(pw1.equals(pw2))
{
jp.setString("Matched");
jp.setStringPainted(true);
}
else jp.setString("Not Matched");
}
}
});

rt.addCaretListener(new CaretListener(){
public void caretUpdate(CaretEvent ce)
{
pw2=new String(rt.getPassword());
jp.setValue(pw2.length());

if(!pw2.isEmpty())
{
if(pw2.equals(pw1))
{
jp.setString("Matched");
jp.setStringPainted(true);
}
else jp.setString("Not Matched");
}
}
});

setExtendedState(MAXIMIZED_BOTH);
}


public static void main(String args[])
{
new PasswordStrengthChecker();
}
}


Password Checker (Strength & Match) Output
Output Window

Password Checking Explanation

CaretListener is what i had used here. Because a CaretEvent is fired when the caret is updated. For a better understand, it is not fired when you press any of the arrow keys instead it fires when you change you press backspace and delete i.e. it is fired when caret is updated w.r.t text. CaretListener usually handles it.

jp.setValue(pw1.length()): Update the value in the progress bar with text. The maximum value of progressbar is set to 10. So it get's filled when the password length reaches 10.
if(!t.getText().isEmpty()): Enter the if-block only if the text in password text area is not empty.
if(pw1.equals(pw2)): If the text in the password field equals the text in the re-type password field, then enter the block.
jp.setString("Matched"): Print on the progress bar that passwords are matched.
jp.setStringPainted(true): The string must be visible, so true is sent. By default it is false.
jp.setString("Not Matched"): If not matched print Not Matched.

The same java code is written for the re-type password field for checking with some changes because no one knows when and on what password field user types.



Posted by at on

Tags: Swing Event Handling Swing Hacks