In a JDialog containing a JOptionPane, how can I set initial focus to a specific JTextField?

Simon Brown

One way I have done this in the past is to add a WindowListener and use the WindowActivated event to request that focus be set on a particular component.

The WindowActivated method gets called when the window is the user's active window and is ready to receive keyboard events.

When the request for focus has been made, I then unregister the window listener so that next time the window is activated, focus is not moved back to the initial component.