Java Swing How to - Bring JFrame to front








Question

We would like to know how to bring JFrame to front.

Answer

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/* w w  w  .  j a v a  2  s  . co m*/
import javax.swing.JFrame;
import javax.swing.Timer;

public class Main extends JFrame {
  private JFrame frame = new JFrame();

  public Main() {
    frame.setSize(600, 600);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.setVisible(true);
    frame.setExtendedState(JFrame.ICONIFIED);
    Timer t = new Timer(3000, new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        java.awt.EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
            frame.setExtendedState(JFrame.NORMAL);
          }
        });
      }
    });
    t.setRepeats(false);
    t.start();
  }

  public static void main(String[] args) {
    new Main();
  }
}