Chapter 8

Test Your Thinking

In this chapter, we investigated how an object may register itself as a listener for particular event types and then act accordingly when events are received.  We discussed various event types, listener interfaces and adapter classes, and you were provided the opportunity to answer questions and demonstrate your newly acquired knowledge of events by writing and modifying actual code.

What was not discussed in this chapter, however, was the ability to register a component as a listener for all events that it fires, without registering listeners for each individual event type and without providing different handler methods for each event.

1. Rewrite FocusApplet from Lab 8.2 so that the active region JPanel uses this new method to register itself for all of its own events, and then print the event type to the applet status bar.

One hint to set you off in the right direction—the method is named enableEvents.  Find the method description within the Java API Documentation and determine how it is used.  Though it is rarely useful to capture all fired events in such a manner, it is yet another tool in your programming toolkit that may come in handy when least expected.

Answer: By consulting the Java API Documentation, you should have been able to determine that the enableEvents method of a component allows you to specify which event types should be sent directly to that component.  In addition, the documentation references the processEvent method—this is the ‘workhorse’ method that receives all the events and takes action upon them.

Let’s start by taking a closer look at the enableEvents method.  This method takes a single argument, which is a bit-mask of events to enable.  Unfortunately, the Java API Documentation is a bit scant on mentioning from where these bit-mask values are derived; you can find all the bit-mask values specified in the java.awt.AWTEvent class as XXX_EVENT_MASK variables, where XXX is an event type for which you would like to register.  As you may recall from you introduction to boolean arithmetic, you can comprise a bit-mask by OR-ing (‘|’) together values.  An example of a bit-mask that registers us for all mouse and mouse-motion events is as follows:

touchpanel.enableEvents (AWTEvent.MOUSE_EVENT_MASK |

                           AWTEvent.MOUSE_MOTION_EVENT_MASK);

If you were only interested in action events, you could call enableEvents as follows:

touchPanel.enableEvents(AWTEvent.ACTION_EVENT_MASK);

To register for all events, simply OR all the specified mask values together. 

So where are all these events being sent, the Twilight Zone?  As you might have expected, they are not sent to the Twilight Zone, but rather to the processEvent method of the component.  To perform actions when events are received, you should override the default processEvent method of your component—In most cases, this means extending the component into your own class.  The Java gurus have many ways of doing this, but our example will use a simple subclass of JPanel defined within the FocusApplet class.

The processEvent method will be called with the single argument being the event that was delivered.  By taking a closer look at the java.awt.AWTEvent class, you should be able to locate methods that allow you to interrogate or print the event type.  Following is our example—we have chosen to use the toString method of the event to display it within the applet status bar.

Though we have registered for all AWT events, it is actually quite silly to do so—many of these events do not apply to the JPanel class, and others are internal events fired for various ‘housekeeping’ reasons.  You would not expect to see such code within an actual ‘live’ program, but we have found it helpful from time-to-time for debugging Java events; you may find it helpful sometime as well.

 

Click here for the FocusApplet.java source code.

 

Click here to run the FocusApplet applet.