Java and event handling

原创 2004年10月31日 13:45:00

Event types
As mentioned above, the Event class is a model of a user interface event. Events naturally fall into categories based on the type of the event (the event type is indicated by the id data member). Table 2 lists all of the events defined by the AWT, sorted by category.

Window events
Window events are generated in response to changes in the state of a window, frame, or dialog.
Event ID
Keyboard events

Keyboard events are generated in response to keys pressed and released while a component has input focus.
Event ID
Mouse events

Mouse events are generated in response to mouse actions occurring within the boundary of a component.
Event ID
Scroll events

Scroll events are generated in response to manipulation of scrollbars.
Event ID
List events

List events are generated in response to selections made to a list.
Event ID
Miscellaneous events

Miscellaneous events are generated in response to a variety of actions.
Event ID

Table 2: Events defined by the AWT, sorted by category

It can be instructive to see event generation in action. The button in Figure 1, when pressed, creates an event browser that displays event information about the events the browser receives. The source code for the event browser is available here.

You need a Java-enabled browser to view this applet

Figure 1: Event generation in action

Event dispatch and propagation
Consider the applet in Figure 2. It consists of two instances of the Button class, embedded within an instance of the Panel class. This instance of the Panel class is itself embedded within another instance of the Panel class. The latter instance of the Panel class sits below an instance of class TextArea, and both instances are embedded within an instance of the Applet class. Figure 3 presents the elements that make up this applet laid out as a tree, with the TextArea and Button instances as the leaves, and an Applet instance as the root. (For more information about the hierarchical layout of components in a user interface, read last month's introduction to the AWT.)

You need a Java-enabled browser to view this applet

Figure 2: Classes embedded within classes


Figure 3: Applet elements tree (hierarchy)

When a user interacts with the applet in Figure 2, the Java run-time system creates an instance of class Event and fills its data members with information describing the action. The Java run-time system then allows the applet to handle the event. It begins with the component that initially received the event (for instance, the button that was clicked) and moves up the component tree, component by component, until it reaches the container at the top of the tree. Along the way, each component has the opportunity to ignore the event or to react to it in one (or more) of the following ways:

  • Modify the data members of the Event instance
  • Take action and perform some computation based on the information contained in the event
  • Indicate to the Java run-time system that the event should propagate no further up the tree

The Java run-time system passes event information to a component via the component's handleEvent() method. All valid handleEvent() methods must be of the form

public boolean handleEvent(Event e)

An event handler requires a single piece of information: a reference to the instance of the Event class containing information about the event that just occurred.

The value returned from the handleEvent() method is important. It indicates to the Java run-time system whether or not the event has been completely handled within the event handler. A true value indicates that the event has been handled and propagation should stop. A false value indicates that the event has been ignored, could not be handled, or has been handled incompletely and should continue up the tree.

Consider the following description of an imaginary user's interaction with the applet in Figure 2. The user clicks on the button labeled "One." The Java language run-time system gathers information about the event (the number of clicks, the location of the click, the time the click occurred, and the component that received the click) and packages that information in an instance of the Event class. The Java run-time system then begins at the component that was clicked (in this case, the Button labeled "One") and, via a call to the component's handleEvent() method, offers the component a chance to react to the event. If the component does not handle the event or handles the event incompletely (indicated by a return value of false), the Java run-time system offers the Event instance to the next higher component in the tree -- in this case an instance of the Panel class. The Java run-time system continues in this manner until the event is handled or the run-time system runs out of components to try. Figure 4 illustrates the path of this event as the applet attempts to handle it.


Figure 4: The path of an event

Each component making up the applet in Figure 2 adds a line to the TextArea object that indicates it received an event. It then allows the event to propagate to the next component in the tree. Listing 1 contains the code for a typical handleEvent() method. The complete source code for this applet is available here.

public boolean handleEvent(Event evt)
   if ( == Event.ACTION_EVENT)
      ta.appendText("Panel " + str + " saw action.../n");
   else if ( == Event.MOUSE_DOWN)
      ta.appendText("Panel " + str + " saw mouse down.../n");

   return super.handleEvent(evt);

Listing 1: A typical handleEvent() method

Event helper methods
The handleEvent() method is one place a programmer can put application code for handling events. Occasionally, however, a component will only be interested in events of a certain type (for example, mouse events). In these cases, the programmer can place the code in a helper method, rather than placing it in the handleEvent() method.

Here is a list of the helper methods available to programmers. There are no helper methods for certain types of events.

action(Event evt, Object what)
gotFocus(Event evt, Object what)
lostFocus(Event evt, Object what)
mouseEnter(Event evt, int x, int y)
mouseExit(Event evt, int x, int y)
mouseMove(Event evt, int x, int y)
mouseUp(Event evt, int x, int y)
mouseDown(Event evt, int x, int y)
mouseDrag(Event evt, int x, int y)
keyDown(Event evt, int key)
keyUp(Event evt, int key)

false to indicate that the helper method did not handle the event.

The implementation of the handleEvent() method provided by class Component invokes each helper method. For this reason, it is important that the redefined implementations of the handleEvent() method in derived classes always end with the statement

return super.handleEvent(e);

The code in Listing 2 illustrates this rule.

public boolean handleEvent(Event e)
   if ( instanceof MyButton)
      // do something...
      return true;

   return super.handleEvent(e);


  • u013412772
  • u013412772
  • 2016年08月14日 13:42
  • 7316

Java and event handling

Event types As mentioned above, the Event class is a model of a user interface event. Events natural...
  • atloan
  • atloan
  • 2004年10月31日 13:45
  • 1156

Android_文档学习_UI_Handling UI Events

Handling UI Events在android中,有多种拦截用户与你的应用程序之间交互的方式.当考虑到你的用户接口事件,倾向是捕捉从具体的与用户交互的View对象中产生的Event.view类提...
  • Jason69181
  • Jason69181
  • 2010年12月13日 14:41
  • 599

<Event Handling Guide for iOS> 读书笔记总结

  • lihei12345
  • lihei12345
  • 2015年01月12日 19:20
  • 1799

事件处理指南(Event Handling Guide for iOS) 阅读笔记 (二) 响应链

Event Delivery: The Responder Chain我们希望在我们的app中可以动态的响应触摸事件.比如一个触摸可能会发生在屏幕上不同的位置和不同的组件上, 我们需要判断哪个组件响应...
  • yusky2008
  • yusky2008
  • 2015年04月26日 16:06
  • 1359

DOM2中的高级事件处理(Advanced Event Handling with DOM Level 2)

导读:    迄今为止,在本章中出现的事件处理技术都是DOM0级的一部分,所有支持JavaScript的浏览器都支持DOM0的API.DOM2定义了高级的事件处理API,和DOM0的API相比,有着令...
  • ycyangcai
  • ycyangcai
  • 2007年11月11日 17:36
  • 849

Handling App Links

参考  参考
  • u012306714
  • u012306714
  • 2016年10月18日 14:37
  • 316

Event Handling Guide for iOS

参考 [1]
  • freshForIphone
  • freshForIphone
  • 2012年11月17日 16:38
  • 800

Event Handling Guide for iOS--(一)--About Events in iOS(翻译)

Event Handling Guide for iOS
  • opentogether
  • opentogether
  • 2016年08月11日 17:00
  • 406

event handling in Java

import javax.swing.*; import java.awt.event.*; public class SimpleGui implements ActionListener { ...
  • sentinel911
  • sentinel911
  • 2015年07月28日 09:03
  • 464
您举报文章:Java and event handling