Monday, February 13, 2012

The AWTEvent class

All events are instances of event classes in the event class hierarchy. All classes in this hierarchy inherit from the java.util.EventObject class, and all classes in the AWT event hierarchy inherit from java.awt.AWTEvent.

The subclasses of AWTEvent can be divided into two groups:
  • low-level events
  • high-level (or semantic) events
low-level events
Low-level events are very specific user actions, such as clicking a button or moving a mouse.
high-level (or semantic) events
High-level, or semantic, events are more generalized or abstract events. They occur when a user chooses, selects, or alters something. High-level events are also known as semantic events because they abstract meaningful actions from sets of lower-level actions.
The ComponentEvent class contains five subclasses representing low-level events. These are
  • InputEvent
  • WindowEvent
  • FocusEvent
  • ContainerEvent
  • PaintEvent
InputEvent
InputEvent is the root event class for all component-level input events. This class has two subclasses - MouseEvent and KeyEvent.

The three types of key events are key pressed, key released, and key typed. Each of these key events has a corresponding method and constant. You can use the getKeyChar method to return the char representing the key that has generated the event. Mouse events occur when a mouse button is pressed or released, or when a mouse is moved. The MouseEvent class also contains the MouseWheelEvent class for mouse wheel rotation.
WindowEvent
Window events occur whenever a window

  • is activated or deactivated
  • is opened, closed, or closing
  • is reduced to an icon or restored to a fully open state
  • gains or loses focus
FocusEvent
The two focus events are focus gained and focus lost. A component gains permanent focus when it makes a successful requestFocus method call or when users click or tab to access it. And it gains temporary focus as a side-effect of another operation, such as a window deactivation or scrollbar drag.

You distinguish between permanent and temporary focus using the isTemporary method of the FocusEvent:

public boolean isTemporary()

The isTemporary method returns true if the focus was temporarily lost.
ContainerEvent
Container events occur when components are added to, or removed from, a container.
The registered container listener receives notification of an event and can obtain the identity of the new component using a getChild method call. This allows containers to easily add input event listeners to, or remove them from, their components as each component is added or removed.
PaintEvent
Paint events are generated by the AWT, not delivered to any listeners. So they are of no practical use to programmers.
The four pre-defined subclasses representing semantic events are
  • ItemEvent
  • AdjustmentEvent
  • TextEvent
  • ActionEvent
ItemEvent
Item events occur in components that have implemented the ItemSelectable interface. They represent the selection or deselection events on selectable items, such as lists, checkboxes, and pop-up menus.

Item events are generated by ItemSelectable objects, for example Checkbox, List, JComboBox, and CheckboxMenuItem. Rather than having to deal with individual mouse or click events, this high-level event is more meaningful and a listener can more easily deal with relevant events.
AdjustmentEvent
Adjustment events occur in components such as scrollbars, which have numeric values that can be incremented or decremented by the user.

For example, as a scrollbar is moved, its new position is signaled to an adjustment listener as an adjustment event.
TextEvent
Text events occur when text is entered, deleted, or edited in text entry fields.
ActionEvent
Action events include such actions as clicking a button or pressing a function key. Several different user actions may result in the same action event being generated, which means that logically-related, higher-level events can be grouped together for handling in one place.

You can use the ActionEvent class's getActionCommand method to distinguish between different types of the same semantic event.
All event types are represented as predefined constants in the different event classes to which they belong. For example, the MouseEvent has constants for MOUSE_CLICKED and MOUSE_RELEASED, whereas for KeyEvent there are such constants as KEY_PRESSED.

You can use the AWTEvent class getID method to return one of these constants and so identify the type of event that has occurred.

1 comments:

Post a Comment