Raji
Raji

Reputation:

JTextField

how to set the focus in the JTextField??

Upvotes: 0

Views: 683

Answers (5)

Pranjal Choladhara
Pranjal Choladhara

Reputation: 915

We can gain focus in any jComponent with grabFocus()

 jtextField.grabGocus();
 jlist.grabFocus();
 jCombobox.grabFocus()
 etc.

Upvotes: 0

user1639485
user1639485

Reputation: 808

All you need is to call grabFocus()
Example: jTextField.grabFocus();

Upvotes: 1

objects
objects

Reputation: 8677

Use requestFocusInWindow()

requestFocus() can be used but is discouraged as it is platform dependent.

See here for more details

Upvotes: 3

Victor
Victor

Reputation: 5807

JTextField extends JComponent so JTextField would have this method.

Upvotes: 0

Darren Hicks
Darren Hicks

Reputation: 5076

The abstract class Component which JTextField extends provides a requestFocus() method.

JTextField jtf = getTextFieldFromSomewhere();
jtf.requestFocus();

Upvotes: 3

Related Questions