Hi,
I have a JTextField in a JPanel and I would like to have JTextField set
to be the default focus with the caret blinking in it.
I have tried requestFocusInWindow(), requestFocus(), grabFocus() and
setFocusable( true) and all failed.
How can I do that?
Arnaud Berger - 05 Apr 2005 07:45 GMT
Hi,
requestFocus should work, though.
see the following quick example :
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Focus extends JFrame {
public Focus(){
JPanel pan =new JPanel();
JTextField field1=new JTextField("11111111");
JTextField field2=new JTextField("222222222");
JTextField field3=new JTextField("333333333");
pan.add(field1);
pan.add(field2);
pan.add(field3);
setContentPane(pan);
pack();
setVisible(true);
field2.requestFocus();
}
public static void main(String[] args) {
new Focus();
}
}
Regards,
Arnaud
> Hi,
>
[quoted text clipped - 5 lines]
>
> How can I do that?
Kari Ikonen - 14 Apr 2005 20:52 GMT
> I have a JTextField in a JPanel and I would like to have JTextField set
> to be the default focus with the caret blinking in it.
>
> I have tried requestFocusInWindow(), requestFocus(), grabFocus() and
> setFocusable( true) and all failed.
First of all: Use requestFocus(), ignore those other methods.
Then quick quesses in the order of likehood:
(a) Call to requestFocus() is done *before* window is visible.
=> requestFocus() fails
(b) non-EDT-thread tries to call requestFocus()
=> requestFocus() fails
(c) Component is not yet attached into layout
=> requestFocus() fails
(d) Component is not visible
=> requestFocus() fails

Signature
KI