/*
 * [TestJPasswordField.java]
 *
 * Summary: demonstrate the use of javax.swing.JPasswordField.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2009-01-01 initial version
 */
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();
                // echo keystrokes in the usual visible way.
                // jp.setEchoChar( ( char ) 0 );
                // echo chars as *
                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 );
                            }
                        // erase password after use
                        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 );
                }
            } );
        } // end main
    }