Java JTextField grab focus on window open event

Description

Java JTextField grab focus on window open event

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JTextField;

public class Main extends JFrame {

  JTextField field1;//w w w .  j a  va 2 s.com

  public Main() {
    field1 = new JTextField(10);
    getContentPane().add("Center", field1);
    addWindowListener(new WindowAdapter() {
      public void windowOpened(WindowEvent e) {
        field1.requestFocus();
      }
    });
    pack();
    setVisible(true);
  }
  public static void main(String[] argv) {
     new Main();
   }
}



PreviousNext

Related