Package shared.dialog

Source Code of shared.dialog.JPasswordDialog

/*
*  JPasswordDialog.java
*
*  Created on 23.02.2010, 21:57:15
*
*  Copyright (C) 23.02.2010, 21:57:15  <reiner>

*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package shared.dialog;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.Timer;


public final class JPasswordDialog
{
   
    private JPasswordDialog()
    {}
   
    public static String showPasswordDialog(Component parent, String title, int passFields, int icon)
    {
        final JPasswordField passwordField = new JPasswordField(passFields);
        JOptionPane pane = new JOptionPane(passwordField, icon, JOptionPane.OK_CANCEL_OPTION);
        JDialog dia = pane.createDialog(parent, title);
        dia.addWindowListener(new WindowAdapter()
        {
            @Override
            public void windowActivated(WindowEvent ev)
            {
                Timer timer = new Timer(50, new ActionListener()
                {
                    public void actionPerformed(ActionEvent ev)
                    {
                       passwordField.requestFocusInWindow();
                    }
                });
                timer.setRepeats(false);
                timer.start();
            }
        });

        dia.setVisible(true);
        return ((Integer)pane.getValue() == JOptionPane.OK_OPTION ? new String (passwordField.getPassword()) : null);
    }
}
TOP

Related Classes of shared.dialog.JPasswordDialog

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.