Java Swing How to - Set the focus on a particular JTextField








Question

We would like to know how to set the focus on a particular JTextField.

Answer

  //from w  w w.j a  va  2s  . c o m

import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main {
  public static void main(String[] argv) throws Exception {
    final JTextField textfield = new JTextField(10);

    
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        textfield.requestFocus();
      }
    });
  }
}