Java Reference
In-Depth Information
There are seven methods in the WindowListener interface:
n
windowActivated( WindowEvent )
n
windowClosed( WindowEvent )
n
windowClosing( WindowEvent )
n
windowDeactivated( WindowEvent )
n
windowDeiconified( WindowEvent )
n
windowIconified( WindowEvent )
n
windowOpened( WindowEvent )
They all take the same form as the windowOpened() method:
public void windowOpened(WindowEvent evt) {
// ...
}
The windowClosing() and windowClosed() methods are similar, but one is called as the
window is closing, and the other is called after it is closed. In fact, you can take action in
a windowClosing() method to stop the window from being closed.
Using Adapter Classes
A Java class that implements an interface must include all its methods, even if it doesn't
plan to do anything in response to some of them.
This requirement can make it necessary to add a lot of empty methods when you're
working with an event-handling interface such as WindowListener , which has seven
methods.
As a convenience, Java offers adapters , Java classes that contain empty do-nothing
implementations of specific interfaces. By subclassing an adapter class, you can imple-
ment only the event-handling methods you need by overriding those methods. The rest
will inherit those do-nothing methods.
The java.awt.event package includes FocusAdapter , KeyAdapter , MouseAdapter ,
MouseMotionAdapter , and WindowAdapter . They correspond to the expected listeners for
focus, keyboard, mouse, mouse motion, and window events.
Listing 12.5 contains a Java application that displays the most recently pressed key, mon-
itoring keyboard events through a subclass of KeyAdapter .
Search WWH ::




Custom Search