Here you can find the source of askPassword(String message)
public static String askPassword(String message)
//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; } }