int java.awt.event.WindowEvent.WINDOW_OPENED : Window Event « Swing Event « Java Tutorial






import java.awt.AWTEvent;
import java.awt.Toolkit;
import java.awt.event.AWTEventListener;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;

public class Main implements AWTEventListener {
  public void eventDispatched(AWTEvent evt) {
    if (evt.getID() == WindowEvent.WINDOW_OPENED) {
      ComponentEvent cev = (ComponentEvent) evt;
      if (cev.getComponent() instanceof JFrame) {
        System.out.println("event: " + evt);
        JFrame frame = (JFrame) cev.getComponent();
        loadSettings(frame);
      }
    }
  }

  public static void loadSettings(JFrame frame) {
    System.out.println("loading");
  }

  public static void saveSettings() {
    System.out.println("saving");
  }

  public static void main(String[] args) throws Exception {
    Toolkit tk = Toolkit.getDefaultToolkit();
    final Main main = new Main();

    tk.addAWTEventListener(main, AWTEvent.WINDOW_EVENT_MASK);

    final JFrame frame = new JFrame("");
    frame.setName("your frame");
    JMenuBar mb = new JMenuBar();
    JMenu menu = new JMenu("File");
    menu.add(new AbstractAction("Quit") {
      public void actionPerformed(ActionEvent evt) {
        try {
          main.saveSettings();
          System.exit(0);
        } catch (Exception ex) {
          System.out.println(ex);
        }
      }
    });
    mb.add(menu);
    frame.setJMenuBar(mb);
    frame.pack();
    frame.setVisible(true);
  }

}








15.41.Window Event
15.41.1.The event IDs for the WindowEvent class
15.41.2.Making a Window Handle its Own EventsMaking a Window Handle its Own Events
15.41.3.The WindowListener Interface: events reflecting changes in the state of a window
15.41.4.Using WindowListenerUsing WindowListener
15.41.5.Using Adapter ClassesUsing Adapter Classes
15.41.6.Listens for window iconify and deiconify events, so that it can stop the animation when the window isn't visible.Listens for window iconify and deiconify events, so that it can stop the animation when the window isn't visible.
15.41.7.WindowListener, WindowFocusListener, WindowStateListener
15.41.8.extends WindowAdapter
15.41.9.Handle a window closing event
15.41.10.Setting the Initial Focused Component in a Window
15.41.11.int java.awt.event.WindowEvent.WINDOW_OPENED