Re: JOptionPane focus



Michael Dunn wrote:
"Ian Wilson" wrote

When I use JOptionPane's static methods to display some JTextFields, initial focus is on one of the buttons, is it possible to set initial focus to the JTextField?

<example code snipped for brevity>

JPanel panel = new JPanel();
final JTextField nameField = new JTextField(10);//<-----final
javax.swing.SwingUtilities.invokeLater(new Runnable() {//<-----add
public void run() { nameField.requestFocusInWindow(); }});//<----add
JPasswordField passwordField = new JPasswordField(10);


Thanks for the quick response Michael, that did the trick.

I can't say I understand this, (e.g. why 'final', how does invokeLater tie in with the chronological sequence in which GUI objects appear and are allocated focus? I'm surprised that the nameField.requestFocus isn't superceded by something similar (e.g. OKButton.requestFocus) being done chronologically later by the JOptionPane constructor. Is there any recommended reading material that might help me understand it?
.