Add key event listener for JPasswordField in Java
Description
The following code shows how to add key event listener for JPasswordField.
Example
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//www . j a v a2s.c om
import javax.swing.JFrame;
import javax.swing.JPasswordField;
public class Main {
public static void main(String args[]) {
JFrame f = new JFrame("JPasswordField Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPasswordField password = new JPasswordField();
password.setEchoChar('#');
f.add(password);
password.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent e) {
System.out.println(new String(password.getPassword()));
}
});
f.setSize(300, 200);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »