This may seem trivial, but I can't figure out how to give the password box in this dialog focus.
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
public class PasswordBox {
@SuppressWarnings("unused")
public String prompt() {
JPasswordField pass = new JPasswordField(10);
int action = JOptionPane.showConfirmDialog(null, pass,"Enter Password",JOptionPane.OK_CANCEL_OPTION);
return new String(pass.getPassword());
}
}
I invoke it from other classes like this: String tmpPASS = new PasswordBox().prompt();
For some reason, when the dialog shows up, the "OK" button gets focus.
stacktrace (see Eng.Fouad's answer):
at javax.swing.JComponent.addNotify(Unknown Source)
at PasswordBox$1.addNotify(PasswordBox.java:14)
at java.awt.Container.addNotify(Unknown Source)