Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOptionPane to get password

JOptionPane can be used to get string inputs from user, but in my case, I want to display a password field in showInputDialog.

The way I need is the input given by the user should be masked and the return value must be in char[]. I need a dialog box with a message, password field, and two buttons. Can that be done? Thanks.

like image 386
Ahamed Avatar asked Jan 16 '12 14:01

Ahamed


People also ask

What does JOptionPane showInputDialog do?

This is a review of the showInputDialog() method of JOptionPane Class. With this method we can prompt the user for input while customizing our dialog window.

Is JOptionPane a GUI?

JOptionPane is a nice way to throw together a few dialog boxes and get a GUI, but it doesn't give us the flexibility we really want. But with power and flexibility come complexity. The basic class we start with is a JFrame. A JFrame is a top level window with a title and a border.


2 Answers

Yes, it is possible using JOptionPane.showOptionDialog(). Something like this:

JPanel panel = new JPanel(); JLabel label = new JLabel("Enter a password:"); JPasswordField pass = new JPasswordField(10); panel.add(label); panel.add(pass); String[] options = new String[]{"OK", "Cancel"}; int option = JOptionPane.showOptionDialog(null, panel, "The title",                          JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,                          null, options, options[1]); if(option == 0) // pressing OK button {     char[] password = pass.getPassword();     System.out.println("Your password is: " + new String(password)); } 
like image 109
Eng.Fouad Avatar answered Sep 28 '22 10:09

Eng.Fouad


The easiest thing is to use JOptionPane's showConfirmDialog method and to pass in a reference to a JPasswordField; e.g.

JPasswordField pf = new JPasswordField(); int okCxl = JOptionPane.showConfirmDialog(null, pf, "Enter Password", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);  if (okCxl == JOptionPane.OK_OPTION) {   String password = new String(pf.getPassword());   System.err.println("You entered: " + password); } 

Edit

Below is an example using a custom JPanel to display a message along with the JPasswordField. Per the most recent comment, I've also (hastily) added code to allow the JPasswordField to gain focus when the dialog is first displayed.

public class PasswordPanel extends JPanel {   private final JPasswordField passwordField = new JPasswordField(12);   private boolean gainedFocusBefore;    /**    * "Hook" method that causes the JPasswordField to request focus the first time this method is called.    */   void gainedFocus() {     if (!gainedFocusBefore) {       gainedFocusBefore = true;       passwordField.requestFocusInWindow();     }   }    public PasswordPanel() {     super(new FlowLayout());      add(new JLabel("Password: "));     add(passwordField);   }    public char[] getPassword() {       return passwordField.getPassword();   }    public static void main(String[] args) {       PasswordPanel pPnl = new PasswordPanel();       JOptionPane op = new JOptionPane(pPnl, JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);        JDialog dlg = op.createDialog("Who Goes There?");        // Wire up FocusListener to ensure JPasswordField is able to request focus when the dialog is first shown.       dlg.addWindowFocusListener(new WindowAdapter() {         @Override         public void windowGainedFocus(WindowEvent e) {             pPnl.gainedFocus();         }       });        if (op.getValue() != null && op.getValue().equals(JOptionPane.OK_OPTION)) {           String password = new String(pPnl.getPassword());           System.err.println("You entered: " + password);       }   } } 
like image 22
Adamski Avatar answered Sep 28 '22 10:09

Adamski