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.

Here are the most commonly used 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 scrollbar)

  • ItemEvent (the user made a selection from a set of checkbox or list items)

Five low-level event classes are commonly used:

  • 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). See page 321 for more information about the focus concept.

  • WindowEvent (the window state changed)

Table 8-1 shows the most important AWT listener interfaces, events, and event sources.

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 

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

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


Event Handling Summary

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 Figure 8-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.

Figure 8-6. Relationship between event sources and listeners


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.

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.


 java.awt.event.ComponentEvent 1.0 

  • Component getComponent()

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


       
    top



    Core Java 2 Volume I - Fundamentals
    Core Java(TM) 2, Volume I--Fundamentals (7th Edition) (Core Series) (Core Series)
    ISBN: 0131482025
    EAN: 2147483647
    Year: 2003
    Pages: 132

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net