Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Chapter 7 Continued: Abstract Window Toolkit Debugging

 
Training Index

[<<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]