I've learned in this other question that it's possible by overriding the addNotify method, but that's not working. Here's my code:
private boolean accessPasswordFrame(String titleText, String labelText, String errorMessage, int accessType) {
JPasswordField passwordField = new JPasswordField() {
public void addNotify() {
super.addNotify();
requestFocus();
}
};
final JComponent[] inputs = new JComponent[] {
new JLabel(labelText),
passwordField
};
int result = JOptionPane.showConfirmDialog(frame, inputs, titleText, JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String rootPass = new String(passwordField.getPassword());
if (accessType == ADMIN_TYPE) {
if (rootPass.equals(ROOT_PASSWORD)) {
return true;
} else {
JOptionPane.showMessageDialog(null,
errorMessage, "Erro",
JOptionPane.ERROR_MESSAGE);
}
} else if (accessType == USER_TYPE) {
if (PasswordFrame.getPasswords() != null) {
for (Map.Entry<String, String> e : PasswordFrame.getPasswords().entrySet()) {
if (rootPass.equals(e.getValue())) {
lastUser = e.getKey();
return true;
} else {
JOptionPane.showMessageDialog(null,
errorMessage, "Erro",
JOptionPane.ERROR_MESSAGE);
}
}
} else {
JOptionPane.showMessageDialog(null,
"Usuários cadastrados não existem.", "Erro",
JOptionPane.ERROR_MESSAGE);
}
}
}
return false;
}
Once the JOptionPane appears, the default focus is on the "Ok" button:
And if I just requestFocus()
before showing the dialog, it doesn't work as well.
You can add a listener to the component so that the listener is invoked when the option pane is made visible and you can then request focus on the component.
Check out RequestFocusListener
class found in Dialog Focus for a reusable solution.