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.
The easiest thing is to use
JOptionPane
'sshowConfirmDialog
method and to pass in a reference to aJPasswordField
; e.g.Edit
Below is an example using a custom
JPanel
to display a message along with theJPasswordField
. Per the most recent comment, I've also (hastily) added code to allow theJPasswordField
to gain focus when the dialog is first displayed.You can create you're own dialog that extends JDialog and then you can put whatever you want in it.
Yes, it is possible using
JOptionPane.showOptionDialog()
. Something like this:This dialogue looks a lot better if you do
Without that you can't see it at all.
Also
should be
Other than that it works great. Thanks for the code. Saved me time facing around with Swing.
Also, if you don't want to leave the dialogue running in the background every time you open it, you'll need to close it with something like
The following class is an extension of Adamski's great answer: