// Purpose. JPasswordField and document model demo import java.awt.*; // BorderLayout import java.awt.event.*; // WindowAdapter, WindowEvent, ActionListener import javax.swing.*; // JPanel, JLabel, JPasswordField, JTextField import javax.swing.text.*; // JTextComponent public class JPasswordFieldDemo extends JPanel { public static void main( String[] args ) { JFrame frame = new JFrame( "JPasswordField demo" ); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { System.exit( 0 ); } } ); JLabel prompt = new JLabel( "Enter password:", SwingConstants.CENTER ); frame.getContentPane().add( prompt, "North" ); JPasswordField password = new JPasswordField( 20 ); password.setEchoChar( '*' ); // password.requestFocus(); frame.getContentPane().add( password, "Center" ); JTextField repeat = new JTextField( 20 ); repeat.setDocument( password.getDocument() ); repeat.requestFocus(); frame.getContentPane().add( repeat, "South" ); ActionListener al = new ActionListener() { public void actionPerformed( ActionEvent e ) { JTextComponent tc = (JTextComponent) e.getSource(); System.out.println( "password is " + tc.getText() ); tc.setText( "" ); } }; password.addActionListener( al ); repeat.addActionListener( al ); frame.pack(); frame.setVisible(true); } }