Java Reference
In-Depth Information
Figure 13.1
Output of the EventDemo program.
The Event Adapter Classes
A listener class must implement a listener interface, meaning that all of the
methods of the interface must be defined in the class. For the listener interfaces
with one method, this is not too much work; however, for the listener inter-
faces with multiple methods, it can be fairly tedious—especially when you
have to define methods in which you are not interested.
For example, suppose that you want to handle the windowClosing() cause
of a WindowEvent so that the program terminates. Then the listener class
needs to implement WindowListener and define all seven of its methods. The
class might look similar to the following:
import java.awt.event.*;
public class TediousWindowCloser implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e)
{}
public void windowClosed(WindowEvent e)
{}
public void windowIconified(WindowEvent e)
{}
public void windowDeiconified(WindowEvent e)
{}
public void windowActivated(WindowEvent e)
{}
Search WWH ::




Custom Search