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?
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.
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.
Would Component.requestFocus()
give you what you need?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With