JPasswordField: getPassword() : JPasswordField « javax.swing « Java by API






JPasswordField: getPassword()

 


import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

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

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

  public Main() {
    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()));
      }
    });
  }
}

   
  








Related examples in the same category

1.new JPasswordField()
2.new JPasswordField(int columns)
3.new JPasswordField(String text, int columns)