Java Reference
In-Depth Information
Note that we call the processWindowEvent() method in the superclass if it is not
the closing event. This is very important as it allows the event to be passed on to any
listeners that have been registered for these events. If we don't call
processWindowEvent() for the superclass, any events that we do not handle will
be lost, because the base class method is normally responsible for passing the event to
the listeners that have been registered to receive it.
If we had not commented out the call to the setDefaultCloseOperation() method, our
processWindowEvent() method would still have been called when the close icon was clicked. In
this case we did not need to call dispose() and exit() ourselves. It would have been taken care of
automatically after our processWindowEvent() method had finished executing. This would be
preferable as it means there would be less code in our program, and the code to handle the default close
action is there in the JFrame class anyway.
Enabling Other Low-level Events
The enableEvents() method is inherited from the Component class. This means that any
component can elect to handle its own events. You just call the enableEvents() method for the
component and pass an argument defining the events you want the component to handle. If you want to
enable more than one type of event for a component, you just combine the event masks from
AWTEvent that we saw earlier by linking them with a bitwise OR. To make our window handle mouse
events as well as window events, you could write:
enableEvents(AWTEvent.WINDOW _ EVENT _ MASK | AWTEvent.MOUSE _ EVENT _ MASK);
Of course, you must now also implement processMouseEvent() for the class. Like the
processWindowEvent() method, this method is protected and has void as a return type. It passes
the event as an argument of type MouseEvent . There are two other methods specific to the Window
class that handle events:
Event Handling Methods
Description
processWindowFocusEvent(
WindowEvent e)
This method is called for any window focus events
that arise as long as such events are enabled for the
window.
processWindowStateEvent(
WindowEvent e)
This method will be called for events arising as a
result of the window changing state.
These methods and the processWindowEvent() method are only available for objects of type
Window , or for a subclass of Window , so don't try to enable window events on other components.
The other event-handling methods that you can override to handle component events are:
Search WWH ::




Custom Search