Add action event listener for JPasswordField in Java
Description
The following code shows how to add action event listener for JPasswordField.
Example
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//from w ww.j a va 2 s . 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("*", 10);
f.add(password);
password.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("Field=" + password.getPassword());
}
});
f.setSize(300, 200);
f.setVisible(true);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Swing »
Java Tutorial »
Swing »