Java Toolkit .addAWTEventListener (AWTEventListener listener, long eventMask)

Syntax

Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask) has the following syntax.

public void addAWTEventListener(AWTEventListener listener,   long eventMask)

Example

In the following code shows how to use Toolkit.addAWTEventListener(AWTEventListener listener, long eventMask) method.


//  w  ww .java  2s  .  c om

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);
  }

}




















Home »
  Java Tutorial »
    java.awt »




BasicStroke
BorderLayout
CardLayout
Color
Cursor
Desktop
DesktopManager
DisplayMode
EventQueue
FlowLayout
FocusTraversalPolicy
Font
FontMetrics
GradientPaint
Graphics
Graphics2D
GraphicsConfiguration
GraphicsDevice
GraphicsEnvironment
GridBagConstraints
GridBagLayout
GridLayout
Image
ItemSelectable
KeyboardFocusManager
LayoutManager
LayoutManager2
Point
Rectangle
Robot
Shape
SplashScreen
SystemColor
SystemTray
TexturePaint
TrayIcon
Toolkit
Transparency