Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Writing Advanced Applications
Chapter 7 Continued: Abstract Window Toolkit Debugging

[<<BACK] [CONTENTS] [NEXT>>]

Before the new Abstract Window Toolkit (AWT) Event mechanism introduced in JDK 1.1, events were received by a component such as a TextField, and propagated upwards to its parent components. This meant you could simply add some diagnostic code to the component's handleEvent or action method to monitor the events as they arrived.

With the introduction of JDK 1.1 and the new system event queue, events are delivered to an event queue instead of the component itself. The events are then dispatched from the System Event queue to event listeners that register to be notified when an event has been dispatched for that object.


Using AWTEventListener

You can use an AWTEventListener to monitor the AWT events from a system event queue. This listener takes an event mask built from an OR operation of the AWTEvents you want to monitor. To obtain a simple list of the AWTEvent events, use the javap -public java.awt.AWTEvent command. This example tracks the mouse and focus events.

Note:
It is advised to not use AWTEventListener in a shipping product as it will degrade system performance

//EventTest.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class EventTest extends JFrame {

  public EventTest() {
    JButton jb1=new JButton("hello");
    getContentPane().add(jb1);

    //AWTEventListener
    getToolkit().addAWTEventListener(
      new AWTEventListener() {
        public void eventDispatched(AWTEvent e) {
          System.out.println(e+"\n");
        }
      }, AWTEvent.MOUSE_EVENT_MASK |
           AWTEvent.FOCUS_EVENT_MASK
       );
  }

  public static void main (String args[]) {

    EventTest et=new EventTest();
    et.setSize(300,300);
    et.pack();
    et.show();
  }
}

[TOP]


[ This page was updated: 13-Oct-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.