package com.mindprod.example;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.System.*;
/**
* demonstrate the use of javax.swing.JPasswordField.
*
* @author Roedy Green, Canadian Mind Products
* @version 1.0 2009-01-01 initial version
* @since 2009-01-01
*/
public final class TestJPasswordField
{
/**
* where to store the password. NOT a String.
*/
private static char[] password;
/**
* Debugging harness for a Frame
*
* @param args command line arguments are ignored.
*/
public static void main( String args[] )
{
SwingUtilities.invokeLater( new Runnable()
{
/**
* } fire up a JFrame on the Swing thread
*/
public void run()
{
final JFrame frame = new JFrame();
final Container contentPane = frame.getContentPane();
final JLabel instructions = new JLabel( "enter your password" );
final JPasswordField jp = new JPasswordField();
jp.setEchoChar( '*' );
jp.setBackground( Color.BLACK );
jp.setForeground( Color.YELLOW );
jp.setFont( new Font( "Dialog", Font.BOLD, 15 ) );
jp.setHorizontalAlignment( JPasswordField.LEFT );
jp.setEnabled( true );
jp.setEditable( true );
jp.addActionListener( new ActionListener()
{
/**
* Invoked when user hits enter after entering entire
* field..
*/
public void actionPerformed( ActionEvent e )
{
password = jp.getPassword();
for ( char c : password )
{
out.print( c );
}
for ( int i = 0; i < password.length; i++ )
{
password[ i ] = 0;
}
password = null;
}
} );
contentPane.add( instructions, BorderLayout.NORTH );
contentPane.add( jp, BorderLayout.CENTER );
frame.pack();
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.validate();
frame.setVisible( true );
}
} );
}
}