user125318 user125318 - 1 month ago 18
Java Question

How to set JTextField as a selected component

I am developing an application which have a JTextField, JButton and JTextArea in same JFrame. How do I set JTextField selected when JFrame starts.

Answer

How do I set JTextField selected when JFrame starts.

I assume you mean you want the text field to have focus so you can start typing into it. ("Selected", in Swing terminology, means the text contained in the text field will be highlighted.)

The first component on the frame (top/left) will gain focus automatically.

If your text field isn't the first component then you can request focus with code like:

frame.setVisible(true);
textField.requestFocusInWindow();

The key is that you need to make the focus request AFTER the frame is visible.