Java ScreenShot
     

Screenshot Core Java 2: Volume I - Fundamentals

Table of Contents
 8.  Event Handling


Semantic and Low-Level Events in the AWT

The AWT makes a useful distinction between low-level and semantic events. A semantic event is one that expresses what the user is doing, such as "clicking that button"; hence, an ActionEvent is a semantic event. Low-level events are those events that make this possible. In the case of a button click, this is a mouse down, a series of mouse moves, and a mouse up (but only if the mouse up is inside the button area). Or it might be a keystroke, which happens if the user selects the button with the TAB key and then activates it with the space bar. Similarly, adjusting a scrollbar is a semantic event, but dragging the mouse is a low-level event. There are four semantic event classes in the java.awt.event package:

  • ActionEvent (for a button click, a menu selection, selecting a list item, or ENTER typed in a text field)
  • AdjustmentEvent (the user adjusted a scroll bar)
  • ItemEvent (the user made a selection from a set of checkbox or list items)
  • TextEvent (the contents of a text field or text area were changed)

There are seven low-level event classes:

  • ComponentEvent (the component was resized, moved, shown, or hidden); also is the base class for all low-level events
  • KeyEvent (a key was pressed or released)
  • MouseEvent (the mouse button was pressed, released, moved, or dragged)
  • MouseWheelEvent (the mouse wheel was rotated)
  • FocusEvent (a component got focus, or lost focus)
  • WindowEvent (the window state changed)
  • ContainerEvent (a component has been added or removed)
Java graphics notes_icon.gif

Mouse wheel events were added in SDK 1.4.

Event Handling Summary

Table 8-1 shows all AWT listener interfaces, events, and event sources. Notice that this table gives a number of events that track the focus of components and the activation of windows—these concepts are explained in the sections that follow. Let's go over the event delegation mechanism one more time to make sure that you understand the relationship between event classes, listener interfaces, and adapter classes. Event sources are user interface components, windows, and menus. The operating system notifies an event source about interesting activities, such as mouse moves and keystrokes. The event source describes the nature of the event in an event object. It also keeps a set of listeners—objects that want to be called when the event happens (see Screenshot-6). The event source then calls the appropriate method of the listener interface to deliver information about the event to the various listeners. The source does this by passing the appropriate event object to the method in the listener class. The listener analyzes the event object to find out more about the event. For example, you can use the getSource method to find out the source, or the getX and getY methods of the MouseEvent class to find out the current location of the mouse.

Screenshot-6. Relationship between event sources and listeners

Java graphics 08fig06.gif


Table 8-1. Event handling summary

Interface

Methods

Parameter/Accessors

Events Generated By

ActionListener

actionPerformed

ActionEvent getActionCommand getModifiers
AbstractButton JComboBox JTextField Timer

AdjustmentListener

adjustmentValueChanged

AdjustmentEvent getAdjustable getAdjustmentType getValue

JScrollbar

ItemListener

itemStateChanged

ItemEvent getItem getItemSelectable getStateChange
AbstractButton JComboBox

TextListener

textValueChanged

TextEvent

 

ComponentListener

componentMoved componentHidden componentResized componentShown
ComponentEvent getComponent

Component

ContainerListener

componentAdded componentRemoved
ContainerEvent getChild getContainer

Container

FocusListener

focusGained focusLost
FocusEvent isTemporary

Component

KeyListener

keyPressed keyReleased keyTyped
KeyEvent getKeyChar getKeyCode getKeyModifiersText getKeyText isActionKey

Component

MouseListener

mousePressed mouseReleased mouseEntered mouseExited mouseClicked
MouseEvent getClickCount getX getY getPoint translatePoint isPopupTrigger

Component

MouseMotionListener

mouseDragged mouseMoved

MouseEvent

Component

MouseWheelListener

mouseWheelMoved

MouseWheelEvent getWheelRotation getScrollAmount

Component

WindowListener

windowClosing windowOpened windowIconified windowDeiconified windowClosed windowActivated windowDeactivated
WindowEvent getWindow

Window

WindowFocusListener

windowGainedFocus windowLostFocus
WindowEvent getOppositeWindow

Window

WindowStateListener

windowStateChanged

WindowEvent getOldState getNewState

Window

Note that there are separate MouseListener and MouseMotionListener interfaces. This is done for efficiency—there are a lot of mouse events as the user moves the mouse around, and a listener that just cares about mouse clicks will not be bothered with unwanted mouse moves. Several interfaces with more than one method have a corresponding adapter class that defines all the methods of the interface to do nothing. You use adapter classes as a time-saving tool: use them when you want to override just a few of the listener interface methods.
Java graphics notes_icon.gif

In the AWT 1.0 event mechanism, events originated in a particular component and were then propagated to all containers of the component. Starting with the AWT 1.1, events are only sent to listeners who registered their interest in receiving them.

The high-level semantic events are quite natural for GUI programming—they correspond to user input. To better understand the low-level events in Table 8-1, we will need to briefly review some terminology.
  • A component is a user interface element such as a button, text field, or scrollbar.
  • A container is a screen area or component that can contain components, such as a window or a panel.

All low-level events inherit from ComponentEvent. This class has a method, called getComponent, which reports the component that originated the event; you can use getComponent instead of getSource. The getComponent method returns the same value as getSource, but already cast as a Component. For example, if a key event was fired because of an input into a text field, then getComponent returns a reference to that text field. A ContainerEvent is generated whenever a component is added or removed. This is quite different from the other events that we saw. Button clicks and keystrokes come from the user in a random fashion, but adding or removing components is a consequence of your programming. Therefore, you don't need an event notification mechanism—you could have programmed the notification yourself. This event was provided to make user interface generators simpler to program. Unless you have a dynamically changing user interface, you will not need to worry about it.

java.awt.event.ComponentEvent 1.0

Java graphics api_icon.gif
  • Component getComponent()

    returns a reference to the component that is the source for the event. This is the same as (Component)getSource().

Screenshot

Java ScreenShot
     
Top
 

Comments