Java JFrame focus component on window open event

Description

Java JFrame focus component 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;//from ww  w .  jav  a2 s .  c  o  m

  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