Password field with key event : JPasswordField « Swing « Java Tutorial






Password field with key event
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class PasswordFieldEchoChar extends JFrame {
  public static void main(String[] args) {
    PasswordFieldEchoChar that = new PasswordFieldEchoChar();
    that.setVisible(true);
  }

  public PasswordFieldEchoChar() {
    setSize(450, 350);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    getContentPane().add(new PasswordPanel(), BorderLayout.SOUTH);
  }
}

class PasswordPanel extends JPanel {
  JPasswordField pwf;

  public PasswordPanel() {
    pwf = new JPasswordField(10);
    pwf.setEchoChar('#');
    add(pwf);

    pwf.addKeyListener(new KeyAdapter() {
      public void keyReleased(KeyEvent e) {
        System.out.println(new String(pwf.getPassword()));
      }
    });
  }
}








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