Обратите внимание, что все вышеперечисленное не выполняется по какой-либо причине в JOptionPane. После долгих проб и ошибок (больше, чем указано выше, 5 минут, во всяком случае) вот что наконец сработало:
final JTextField usernameField = new JTextField();
// ...
usernameField.addAncestorListener(new RequestFocusListener());
JOptionPane.showOptionDialog(this, panel, "Credentials", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE, null, null, null);
public class RequestFocusListener implements AncestorListener {
@Override
public void ancestorAdded(final AncestorEvent e) {
final AncestorListener al = this;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final JComponent component = e.getComponent();
component.requestFocusInWindow();
component.removeAncestorListener(al);
}
});
}
@Override
public void ancestorMoved(final AncestorEvent e) {
}
@Override
public void ancestorRemoved(final AncestorEvent e) {
}
}