FileDocCategorySizeDatePackage
EventTester2.javaAPI DocExample7763Mon Sep 22 13:30:30 BST 1997None

EventTester2

public class EventTester2 extends Frame
A program that displays all the event that occur in its window

Fields Summary
protected Vector
lines
The list of lines to display in the window
Constructors Summary
public EventTester2()
The constructor: register the event types we are interested in

    super("Event Tester");
    this.enableEvents(AWTEvent.MOUSE_EVENT_MASK |
                      AWTEvent.MOUSE_MOTION_EVENT_MASK |
                      AWTEvent.KEY_EVENT_MASK |
                      AWTEvent.FOCUS_EVENT_MASK |
                      AWTEvent.COMPONENT_EVENT_MASK |
                      AWTEvent.WINDOW_EVENT_MASK);
  
Methods Summary
public static voidmain(java.lang.String[] args)
The main method: create an EventTester frame, and pop it up

    EventTester2 et = new EventTester2();
    et.setSize(500, 400);
    et.show();
  
protected java.lang.Stringmousemods(java.awt.event.MouseEvent e)
Return a string representation of the modifiers for a MouseEvent. Note that the methods called here are inherited from InputEvent.

    int mods = e.getModifiers();
    String s = "";
    if (e.isShiftDown()) s += "Shift ";
    if (e.isControlDown()) s += "Ctrl ";
    if ((mods & InputEvent.BUTTON1_MASK) != 0) s += "Button 1 ";
    if ((mods & InputEvent.BUTTON2_MASK) != 0) s += "Button 2 ";
    if ((mods & InputEvent.BUTTON3_MASK) != 0) s += "Button 3 ";
    return s;
  
public voidpaint(java.awt.Graphics g)
This method repaints the text in the window

    for(int i = 0; i < lines.size(); i++)
      g.drawString((String)lines.elementAt(i), 20, i*16 + 50);
  
public voidprocessComponentEvent(java.awt.event.ComponentEvent e)
Display Component events.

    switch(e.getID()) {
    case ComponentEvent.COMPONENT_MOVED: showLine("COMPONENT_MOVED"); break;
    case ComponentEvent.COMPONENT_RESIZED: showLine("COMPONENT_RESIZED");break;
    case ComponentEvent.COMPONENT_HIDDEN: showLine("COMPONENT_HIDDEN"); break;
    case ComponentEvent.COMPONENT_SHOWN: showLine("COMPONENT_SHOWN"); break;
    }
  
public voidprocessFocusEvent(java.awt.event.FocusEvent e)
Display keyboard focus events. Focus can be permanently gained or lost, or temporarily transferred to or from a component.

    if (e.getID() == FocusEvent.FOCUS_GAINED)
      showLine("FOCUS_GAINED" + (e.isTemporary()?" (temporary)":""));
    else
      showLine("FOCUS_LOST" + (e.isTemporary()?" (temporary)":""));
  
public voidprocessKeyEvent(java.awt.event.KeyEvent e)
Display keyboard events. Note that there are three distinct types of key events, and that key events are reported by key code and/or Unicode character. KEY_PRESSED and KEY_RELEASED events are generated for all key strokes. KEY_TYPED events are only generated when a key stroke produces a Unicode character; these events do not report a key code. If isActionKey() returns true, then the key event reports only a key code, because the key that was pressed or released (such as a function key) has no corresponding Unicode character. Key codes can be interpreted by using the many VK_ constants defined by the KeyEvent class, or they can be converted to strings using the static getKeyText() method as we do here.

    String eventtype, modifiers, code, character;
    switch(e.getID()) {
    case KeyEvent.KEY_PRESSED:  eventtype = "KEY_PRESSED"; break;
    case KeyEvent.KEY_RELEASED: eventtype = "KEY_RELEASED"; break;
    case KeyEvent.KEY_TYPED:    eventtype = "KEY_TYPED"; break;
    default: eventtype = "UNKNOWN";
    }

    // Convert the list of modifier keys to a string
    modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());

    // Get string and numeric versions of the key code, if any.
    if (e.getID() == KeyEvent.KEY_TYPED) code = "";
    else code = "Code=" + KeyEvent.getKeyText(e.getKeyCode()) +
           " (" + e.getKeyCode() + ")";

    // Get string and numeric versions of the Unicode character, if any.
    if (e.isActionKey()) character = "";
    else character = "Character=" + e.getKeyChar() +
           " (Unicode=" + ((int)e.getKeyChar()) + ")";

    // Display it all.
    showLine(eventtype + ": " + modifiers + " " + code + " " + character);
  
public voidprocessMouseEvent(java.awt.event.MouseEvent e)
Display mouse events that don't involve mouse motion. The mousemods() method prints modifiers, and is defined below. The other methods return additional information about the mouse event. showLine() displays a line of text in the window. It is defined at the end of this class, along with the paint() method.

    String type = null;
    switch(e.getID()) {
    case MouseEvent.MOUSE_PRESSED:   type = "MOUSE_PRESSED"; break;
    case MouseEvent.MOUSE_RELEASED:  type = "MOUSE_RELEASED"; break;
    case MouseEvent.MOUSE_CLICKED:   type = "MOUSE_CLICKED"; break;
    case MouseEvent.MOUSE_ENTERED:   type = "MOUSE_ENTERED"; break;
    case MouseEvent.MOUSE_EXITED:    type = "MOUSE_EXITED"; break;
    }
    showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] " +
             "num clicks = " + e.getClickCount() +
             (e.isPopupTrigger()?"; is popup trigger":""));
  
public voidprocessMouseMotionEvent(java.awt.event.MouseEvent e)
Display mouse moved and dragged mouse event. Note that MouseEvent is the only event type that has two methods, two EventListener interfaces and two adapter classes to handle two distinct categories of events. Also, as seen in init(), mouse motion events must be requested separately from other mouse event types.

    String type = null;
    switch(e.getID()) {
    case MouseEvent.MOUSE_MOVED:   type = "MOUSE_MOVED"; break;
    case MouseEvent.MOUSE_DRAGGED: type = "MOUSE_DRAGGED"; break;
    }
    showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] " +
             "num clicks = " + e.getClickCount() +
             (e.isPopupTrigger()?"; is popup trigger":""));
  
public voidprocessWindowEvent(java.awt.event.WindowEvent e)
Display Window events. Note the special handling of WINDOW_CLOSING

    switch(e.getID()) {
    case WindowEvent.WINDOW_OPENED: showLine("WINDOW_OPENED"); break;
    case WindowEvent.WINDOW_CLOSED: showLine("WINDOW_CLOSED"); break;
    case WindowEvent.WINDOW_CLOSING: showLine("WINDOW_CLOSING"); break;
    case WindowEvent.WINDOW_ICONIFIED: showLine("WINDOW_ICONIFIED"); break;
    case WindowEvent.WINDOW_DEICONIFIED: showLine("WINDOW_DEICONIFIED"); break;
    case WindowEvent.WINDOW_ACTIVATED: showLine("WINDOW_ACTIVATED"); break;
    case WindowEvent.WINDOW_DEACTIVATED: showLine("WINDOW_DEACTIVATED"); break;
    }

    // If the user requested a window close, quit the program.
    // But first display a message, force it to be visible, and make
    // sure the user has time to read it.
    if (e.getID() == WindowEvent.WINDOW_CLOSING) {
      showLine("WINDOW_CLOSING event received.");
      showLine("Application will exit in 5 seconds");
      update(this.getGraphics());
      try {Thread.sleep(5000);} catch (InterruptedException ie) { ; }
      System.exit(0);
    }
  
protected voidshowLine(java.lang.String s)
Add a new line to the list of lines, and redisplay


              
      
    if (lines.size() == 20) lines.removeElementAt(0);
    lines.addElement(s);
    repaint();