Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set a focus on JTextField in Swing?

Tags:

java

focus

swing

I created a form using Swing in Java. In the form I have used a JTextField on which I have to set the focus whenever I press a key. How do I set focus on a particular component in Swing?

like image 642
om. Avatar asked Sep 15 '09 06:09

om.


People also ask

How do you set focus on textField?

For me the easiest way to get it to work, is to put the component. requestFocus(); line, after the setVisible(true); line, at the bottom of your frame or panel constructor.

Which method is used to set or change the text in JTextField?

The following code creates and sets up the text field: textField = new JTextField(20); The integer argument passed to the JTextField constructor, 20 in the example, indicates the number of columns in the field.


1 Answers

Would Component.requestFocus() give you what you need?

like image 171
phunehehe Avatar answered Sep 22 '22 20:09

phunehehe