Add action listener to JPasswordField : JPasswordField « Swing « Java Tutorial






import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JPasswordField;

public class Main extends JFrame {
  JPasswordField field = new JPasswordField("*", 10);

  public Main() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    field.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
        System.out.println("Field=" + field.getText());
      }
    });

    getContentPane().add(field);
    pack();
    setVisible(true);
  }

  public static void main(String arg[]) {
    new Main();
  }
}








14.17.JPasswordField
14.17.1.Using JPasswordFieldUsing JPasswordField
14.17.2.Password field with key eventPassword field with key event
14.17.3.Add action listener to JPasswordField
14.17.4.Using Actions with Text Components: JPasswordField
14.17.5.Customizing a JPasswordField Look and Feel