Java Reference
In-Depth Information
Handling low-level events is best done by specific components, such as when writing
new lightweight components (a topic I discuss in Chapter 9). If you need to process low-
level events, you can do so by following these steps:
1. Enable receipt of the appropriate events by invoking enableEvents and passing a
constant for the kind of events the applet wants to receive.
2. Override either processEvent or one of its delegates ( processComponentEvent ,
processFocusEvent , processKeyEvent , processMouseEvent , processMouseMotionEvent ,
processInputMethodEvent , or processMouseWheelEvent ) to handle any incoming
events.
3. Disable event receipt by invoking disableEvents when you don't want to receive
any events.
By far the simplest way to handle events, however, is to extend the component to
include a specific listener for a high-level event, and then implement the corresponding
listener. The Java event model—used by both the AWT and Swing, which is the basis for
the AGUI—includes listener interfaces for a variety of events, including the following:
ActionListener : Handles high-level actions generated by components
FocusListener : Handles focus changes in the java.awt.Container and Container
subclasses
KeyListener : Handles individual keyboard events
MouseListener , MouseMotionListener , and MouseWheelListener : Handle mouse- and
pointer-generated events
TextListener : Handles events that indicate when a text field's contents have
changed
Listing 10-9 shows a simple applet that responds to button clicks on its button.
Listing 10-9. Responding to Button Clicks
package com.apress.rischpater.buttonsample;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
 
Search WWH ::




Custom Search