Java Swing Tutorial - Java WindowEvent WINDOW_ACTIVATED








Syntax

WindowEvent.WINDOW_ACTIVATED has the following syntax.

public static final int WINDOW_ACTIVATED

Example

In the following code shows how to use WindowEvent.WINDOW_ACTIVATED field.

import java.awt.event.WindowEvent;
/*from   ww w.jav a  2  s .co m*/
import javax.swing.JFrame;

public class Main extends JFrame {
  public Main() {

    setDefaultCloseOperation(EXIT_ON_CLOSE);
    enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK);
  }

  protected void processWindowEvent(WindowEvent e) {
    if (e.getID() == WindowEvent.WINDOW_ACTIVATED) {
      System.out.println(WindowEvent.WINDOW_ACTIVATED);
      dispose();
      System.exit(0);
    }
    super.processWindowEvent(e); // Pass on the event
  }

  public static void main(String[] a) {
    Main window = new Main();
    window.setBounds(30, 30, 300, 300);
    window.setVisible(true);
  }
}