Java JPasswordField.getPassword()
Syntax
JPasswordField.getPassword() has the following syntax.
public char[] getPassword()
Example
In the following code shows how to use JPasswordField.getPassword() method.
import java.awt.BorderLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
//from ww w . j av a2 s . com
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()));
}
});
}
}
Home »
Java Tutorial »
javax.swing »
Java Tutorial »
javax.swing »