How can I implement a password field like JPasswordField with AWT?

Davanum Srinivas

java.awt.TextField has a setEchoChar which can be used to implement a password field. Here's some sample code.

import java.awt.*;
import java.awt.event.*;

public class PasswordDemo extends Frame {
    public PasswordDemo() {
        TextField password = new TextField(10);
        password.setEchoChar('*');
        add(password);
    }
    public static void main(String[] argv) {
        final PasswordDemo f = new PasswordDemo();
        f.pack();
        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
}
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact