Shows a dialog for obtaining a single line of input with obscured echo. - Java Swing

Java examples for Swing:JOptionPane

Description

Shows a dialog for obtaining a single line of input with obscured echo.

Demo Code


import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkEvent.EventType;
import javax.swing.event.HyperlinkListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.URL;
import java.util.Arrays;

public class Main{
    private static final Config config = Config.getInstance();
    /**/*from  w w w.  ja  v  a2 s .co  m*/
     * Shows a dialog for obtaining a single line of input with obscured echo.
     * The window title is "Password".
     *
     * @param prompt the string to prompt the user with
     */
    public static String showPasswordDialog(final String prompt) {
        return showPasswordDialog("Password", prompt);
    }
    /**
     * Shows a dialog for obtaining a single line of input with obscured echo.
     * The window title can be specified.
     *
     * @param title  window title
     * @param prompt the string to prompt the user with
     */
    public static String showPasswordDialog(final String title,
            final String prompt) {
        return showPasswordDialog(parent, title, prompt, null);
    }
    /**
     * Shows a dialog for obtaining a single line of input with obscured echo.
     * The window title can be specified.
     *
     * @param title   window title
     * @param prompt  the string to prompt the user with
     * @param content the default contents of the input field
     */
    public static String showPasswordDialog(final String title,
            final String prompt, final String content) {
        return showPasswordDialog(parent, title, prompt, content);
    }
    /**
     * Shows a dialog for obtaining a single line of input with obscured echo.
     * The window title can be specified.
     *
     * @param parent  the parent for the dialog
     * @param title   window title
     * @param prompt  the string to prompt the user with
     * @param content the default contents of the input field
     */
    public static String showPasswordDialog(final Component parent,
            final String title, final String prompt, final String content) {
        if (!config.isGUIEnabled()) { //text only
            System.out.println("Password: " + title);
            char[] result = null;
            while (result == null) {
                try {
                    result = getPassword(System.in, prompt);
                } catch (IOException ioe) {
                    System.err.println(ioe.toString());
                }
            }
            return new String(result);
        } else { //normal dialog
            final JPasswordField text = new JPasswordField(content);
            text.setEchoChar('*');
            JOptionPane pane = new JOptionPane(
                    new Object[] { prompt, text },
                    JOptionPane.QUESTION_MESSAGE,
                    JOptionPane.OK_CANCEL_OPTION);
            pane.setWantsInput(false);
            JDialog dialog = pane.createDialog(parent, title);
            dialog.pack();
            dialog.setVisible(true);
            Integer value = (Integer) pane.getValue();
            if (value == null
                    || value.intValue() == JOptionPane.CANCEL_OPTION
                    || value.intValue() == JOptionPane.CLOSED_OPTION) {
                return null;
            }
            return new String(text.getPassword());
        }
    }
    /**
     * Prompts the user for a password and attempts to mask input with "*"
     *
     * @param in     stream to be used (e.g. System.in)
     * @param prompt The prompt to display to the user.
     * @return The password as entered by the user.
     */
    @SuppressWarnings("fallthrough")
    public static final char[] getPassword(InputStream in,
            final String prompt) throws IOException {
        MaskingThread maskingthread = new MaskingThread(prompt);
        Thread thread = new Thread(maskingthread, "MaskingThread");
        thread.start();

        char[] lineBuffer;
        char[] buf;
        int i;

        buf = lineBuffer = new char[128];

        int room = buf.length;
        int offset = 0;
        int c;

        loop: while (true) {
            switch (c = in.read()) {
            case -1:
            case '\n':
                break loop;

            case '\r':
                int c2 = in.read();
                if ((c2 != '\n') && (c2 != -1)) {
                    if (!(in instanceof PushbackInputStream)) {
                        in = new PushbackInputStream(in);
                    }
                    ((PushbackInputStream) in).unread(c2);
                } else {
                    break loop;
                }

            default:
                if (--room < 0) {
                    buf = new char[offset + 128];
                    room = buf.length - offset - 1;
                    System.arraycopy(lineBuffer, 0, buf, 0, offset);
                    Arrays.fill(lineBuffer, ' ');
                    lineBuffer = buf;
                }
                buf[offset++] = (char) c;
                break;
            }
        }
        maskingthread.stopMasking();
        if (offset == 0) {
            return null;
        }
        char[] ret = new char[offset];
        System.arraycopy(buf, 0, ret, 0, offset);
        Arrays.fill(buf, ' ');
        return ret;
    }
}

Related Tutorials