Using Components, the GUI Building Blocks |
In the 1.1 AWT event model, events are generated by event sources. One or more listeners can register to be notified about events of a particular kind from a particular source. Sometimes this model is called delegation, since it allows the programmer to delegate authority for event handling to any object that implements the appropriate listener interface. The 1.1 AWT event model lets you both handle and generate AWT events.Event handlers can be instances of any class. As long as a class implements an event listener interface, its instances can handle events. In every program that has an event handler, you'll see three bits of code:
- In the
class
statement of the event handler, code declaring that the class implements a listener interface (or extends a class that implements a listener interface). For example:public class MyClass implements ActionListener {
- Code that registers an instance of the event handling class as a listener upon one or more components. For example:
someComponent.addActionListener(instanceOfMyClass);
- The implementation of the methods in the listener interface. For example:
public void actionPerformed(ActionEvent e) { ...//code that reacts to the action... }A Simple Example
Here is a bare-bones 1.1 applet that illustrates event handling. It contains a single button that beeps when you click it.
You can find the entire program in
Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1-compliant browser, such as HotJava or the JDK Applet Viewer (appletviewer
).Beeper.java
. Here's just the code that implements the event handling for the button:Isn't that simple? Thepublic class Beeper ... implements ActionListener { ... //where initialization occurs: button.addActionListener(this); ... public void actionPerformed(ActionEvent e) { ...//Make a beep sound... } }Beeper
class implements theActionListener
interface, which contains one method:actionPerformed
. SinceBeeper
implementsActionListener
, aBeeper
object can register as a listener for the action events that buttons generate. Once theBeeper
has been registered using theButton
addActionListener
method, theBeeper
'sactionPerformed
method is called every time the button is clicked.A More Complex Example
The 1.1 event model, which you saw at its simplest in the above example, is quite powerful and flexible. Any number of event listener objects can listen for all kinds of events from any number of event source objects. For example, a program might create one listener per event source. Or a program might have a single listener for all events from all sources. A program can even have more than one listener for a single kind of event from a single event source.The following applet gives an example of using multiple listeners per object. The applet contains two event sources (
Button
instances) and two event listeners. One of the event listeners (an instance of a class calledMultiListener
) listens for events from both buttons. When it receives an event, it adds the event's "action command" (the text on the button's label) to the top text area. The second event listener (an instance of a class calledEavesdropper
) listens for events on only one of the buttons. When it receives an event, it adds the action command to the bottom text area.
You can find the entire program in
Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1-compliant browser, such as HotJava or the JDK Applet Viewer (appletviewer
).MultiListener.java
. Here's just the code that implements the event handling for the button:In the above code, bothpublic class MultiListener ... implements ActionListener { ... //where initialization occurs: button1.addActionListener(this); button2.addActionListener(this); button2.addActionListener(new Eavesdropper(bottomTextArea)); } public void actionPerformed(ActionEvent e) { topTextArea.append(e.getActionCommand() + "\n"); } } class Eavesdropper implements ActionListener { ... public void actionPerformed(ActionEvent e) { myTextArea.append(e.getActionCommand() + "\n"); } }MultiListener
andEavesdropper
implement theActionListener
interface and register as action listeners using theButton
addActionListener
method. Both classes' implementations of theactionPerformed
method are similar: they simply add the event's action command to a text area.An Example of Handling Another Event Type
So far, the only kind of event you've seen has been action events. Let's take a look at a program that handles another kind of event: mouse events.The following applet displays a rectangle-edged area and a text area. Whenever a mouse event -- a click, press, release, enter, or exit -- occurs on either the rectangle-edged area (
BlankArea
MouseEventDemo), the text area displays a string describing the event.
You can find the entire program in
Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1-compliant browser, such as HotJava or the JDK Applet Viewer (appletviewer
).MouseEventDemo.java
andBlankArea.java
. Here's just the code that implements the event handling:You'll see the code explained in Implementing a Mouse Listener, later in this section.public class MouseEventDemo ... implements MouseListener { ... //where initialization occurs: //Register for mouse events on blankArea and applet (panel). blankArea.addMouseListener(this); addMouseListener(this); } public void mousePressed(MouseEvent e) { saySomething("Mouse button press", e); } public void mouseReleased(MouseEvent e) { saySomething("Mouse button release", e); } public void mouseEntered(MouseEvent e) { saySomething("Cursor enter", e); } public void mouseExited(MouseEvent e) { saySomething("Cursor exit", e); } public void mouseClicked(MouseEvent e) { saySomething("Mouse button click", e); } void saySomething(String eventDescription, MouseEvent e) { textArea.append(eventDescription + " detected on " + e.getComponent().getClass().getName() + ".\n"); textArea.setCaretPosition(maxInt); //hack to scroll to bottom } }
Using Components, the GUI Building Blocks |