grabFocus() when show JDialog.

P

Pierre

Hello,
I have a JDialog with a JTextField in my application. When I show the
dialog, I want the JTextField to grab focus. Very simple... but my
code doesn't work! the first time I show the dialog the textfield has
focus, but after it lost it. here an extract of my code :

public MyDialog(){
super("test");
init();
//search is my JTextField
search.grabFocus();
}

public void show(){
update();
search.grabFocus();
super.show();
}

Any idea why this code doesn't work properly ?

Thanks.
Pierre.
 
P

Paul Lutus

Pierre said:
Hello,
I have a JDialog with a JTextField in my application. When I show the
dialog, I want the JTextField to grab focus. Very simple... but my
code doesn't work! the first time I show the dialog the textfield has
focus, but after it lost it. here an extract of my code :

public MyDialog(){
super("test");
init();
//search is my JTextField
search.grabFocus();
}

public void show(){
update();
search.grabFocus();
super.show();
}

Any idea why this code doesn't work properly ?

Use the SwingUtilities.invokeLater() method to request focus, this increases
the likelihood of success.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top