Java JOptionPane Message askPassword(String message)

Here you can find the source of askPassword(String message)

Description

ask Password

License

Apache License

Declaration

public static String askPassword(String message) 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

public class Main {
    public static String askPassword(String message) {
        JLabel wLabel = new JLabel(message);
        final JPasswordField wPwd = new JPasswordField();

        JOptionPane jop = new JOptionPane(new Object[] { wLabel, wPwd }, JOptionPane.QUESTION_MESSAGE,
                JOptionPane.OK_CANCEL_OPTION);

        JDialog dialog = jop.createDialog(null, "Password");

        dialog.addComponentListener(new ComponentAdapter() {
            @Override//from   ww w  . ja v a2s.c om
            public void componentShown(ComponentEvent e) {
                wPwd.requestFocusInWindow();
            }
        });

        dialog.setVisible(true);
        dialog.dispose();

        int result = (Integer) jop.getValue();

        char[] pwd = null;

        if (result == JOptionPane.OK_OPTION)
            pwd = wPwd.getPassword();

        if (pwd == null)
            return null;

        String password = new String(pwd);

        for (int i = 0; i < pwd.length; i++)
            pwd[i] = 0;

        return password;
    }
}

Related

  1. alert(Component component, String title, String msg)
  2. browse(String url, Component msgParent)
  3. browse(String url, Component msgParent)
  4. chooseIndex(String message, String messageTitle, Object[] objects, Object initialObject)
  5. chooseObject(String message, String messageTitle, Object[] objects, Object initialObject)