The AWT Event Hierarchy Core Java

Having given you a taste of how event handling works, we finish this chapter with an overview of the AWT event handling architecture.

As we briefly mentioned earlier, event handling in Java is object oriented, with all events descending from the EventObject class in the java .util package. (The common superclass is not called Event because that is the name of the event class in the old event model. Although the old model is now deprecated, its classes are still a part of the Java library.)

The Event Object class has a subclass AWTEvent, which is the parent of all AWT event classes.

Inheritance diagram of AWT event classes

Inheritance diagram of AWT event classes

Some of the Swing components generate event objects of yet more event types; these directly extend Event Object, not AWT Event.

The event objects encapsulate information about the event that the event source communicates to its listeners. When necessary, you can then analyze the event objects that passed to the listener object, as we did in the button example with the getSource and get Action Command methods.

Some of the AWT event classes are of no practical use for the Java programmer. For example, the AWT inserts Paint Event objects into the event queue, but these objects are not delivered to listeners. Java programmers don’t listen to paint events; they override the paint Component method to control repainting. The AWT also generates a numberof events that are needed only by system programmers, to provide input systems for ideographic languages, automated testing robots, and so on. We do not discuss these specialized event types.

Semantic and Low-Level Events

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 Action-Event 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 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 typedin 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)
  • WindowEvent (the window state changed)
The following interfaces listen to these events:

Several of the AWT listener interfaces, namely, those that have more than one method, come with a companion adapter class that implements all the methods in the interface to do nothing. (The other interfaces have only a single method each, so there is no benefit in having adapter classes for these interfaces.) Here are the commonly used adapter classes:

Table below shows the most important AWT listener interfaces, events, and event sources.

The javax.swing.event package contains additional events that are specific to Swing components. We cover some of them in the next section

Event Handling Summary

Event Handling SummaryEvent Handling SummaryEvent Handling Summary

This concludes our discussion of AWT event handling. The next chapter shows you how to put together the most common of the components that Swing offers, along with a detailed coverage of the events they generate.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

Core Java Topics