Java Reference
In-Depth Information
public void windowDeactivated(WindowEvent e)
{}
}
The TediousWindowCloser class must define all seven methods of Win-
dowListener, even though six of them do not have any implementation. There
is nothing wrong with this class except that it was a lot of typing for really only
defining one method.
This is where the adapter classes come in. The event listener interfaces that
contain more than one method have a corresponding event adapter class that
implements the interface and defines each method in the interface with an
empty method body. Instead of implementing an event listener interface, you
can extend the corresponding event adapter class and define only those meth-
ods in which you are interested.
The WindowAdapter class is an example of an event adapter class. It imple-
ments WindowListener, and defines its seven methods with empty method
bodies. Other event adapter classes in the AWT and Swing include:
ComponentAdapter. Defines the four methods of ComponentListener.
ContainerAdapter. Defines the two methods of ContainerListener.
FocusAdapter. Defines the two methods of FocusListener.
KeyAdapter. Defines the three methods of KeyListener.
MouseAdapter. Defines the five methods of MouseListener.
MouseMotionAdapter.
Defines the two methods of
MouseMotionListener.
InternalFrameAdapter.
Defines the seven methods of
InternalFrameListener.
MouseInputAdapter.
Defines the seven methods of MouseInputListener.
To demonstrate using an adapter class, the following SimpleWindowCloser
class extends WindowAdapter instead of implementing WindowListener. The
class fulfills the same purpose as the TediousWindowCloser class, but notice
that it is simpler to write:
import java.awt.event.*;
public class SimpleWindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}
Because the SimpleWindowCloser class extends WindowAdapter and Win-
dowAdapter implements WindowListener, that makes SimpleWindowCloser a
Search WWH ::




Custom Search