**********************************************************************
COMPLETE SOURCE CODE FOR : SetPasswordLength.java
**********************************************************************
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.FlowLayout;
public class SetPasswordLength implements CaretListener
{
//Create password field
JPasswordField passwordField=new JPasswordField(12);
JLabel label=new JLabel("Press Backspace to delete password");
JFrame frame;
//Create key listener that will listen when someone press Backspace key
KeyListener kl=new KeyAdapter()
{
public void keyPressed(KeyEvent event)
{
if(event.getKeyCode()==KeyEvent.VK_BACK_SPACE)
{
passwordField.setEditable(true);
label.show(false);
frame.repaint();
frame.validate();
}
}
};
public SetPasswordLength()
{
label.show(false);
passwordField.addCaretListener(this);
passwordField.addKeyListener(kl);
frame=new JFrame("Set password length");
frame.setLayout(new FlowLayout());
frame.add(passwordField);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,100);
frame.setVisible(true);
}
//Listen for changes in the caret position
public void caretUpdate(CaretEvent event)
{
int passwordLength=passwordField.getText().length();
//If password length equal to five characters, password field will uneditable
//You can change to any password length that you want by change 5 to any other value
if(passwordLength==5)
{
passwordField.setEditable(false);
label.show(true);
frame.repaint();
frame.validate();
}
}
public static void main(String[]args)
{
SetPasswordLength spl=new SetPasswordLength();
}
}
**********************************************************************
JUST COMPILE AND EXECUTE IT
**********************************************************************