Java Reference
In-Depth Information
82
//overriding the windowClosing() method will allow the user to click the Close
button
83
addWindowListener (
84
new WindowAdapter ()
85
{
86
public void windowClosing ( WindowEvent e )
87
{
88
System .exit ( 0 ) ;
89
}
90
}
91
) ;
92
} //end of constructor method
93
FIGURE 5-29
As shown in Figure 5-29, the addWindowListener() method includes an argu-
ment inside the parentheses; the argument extends through line 91 with the clos-
ing parenthesis and the semicolon. The argument creates a new occurrence of the
WindowAdapter class. Adapter classes provide prewritten methods for inter-
faces. Programmers can override the methods of interest and ignore the others.
In this particular case, WindowAdapter is an adapter class with ten window
event-handling methods. Table 5-12 displays the ten methods and how they are
triggered.
Table 5-12 Window Event-Handling Methods
WINDOW EVENT-HANDLING METHOD
HOW THE EVENT IS TRIGGERED
windowActivated()
User starts the application or applet
windowClosed()
Operating system has closed the window
windowClosing()
User clicks the Close button
windowDeactivated()
User clicks in the application to give it the focus
windowDeiconified()
User clicks a minimized button on the taskbar
windowGainedFocus()
User clicks anywhere in the window (or
programmed operation selects window)
windowIconified()
User clicks the Minimize button
windowLostFocus()
User clicks away from the window (or
programmed operation causes another window
to gain focus)
windowOpened()
Operating system has opened the window
windowStateChanged()
Invoked when the window's state changes by
virtue of being iconified, maximized, etc.
Each method in the WindowAdapter class is an event listener for a Window
or a Frame event. In Java, programmers have the choice of implementing the
WindowListener interface and then writing all the methods themselves, or using
WindowAdapter and providing only the methods they wish to change or
override. It is appropriate to use the WindowAdapter in this case because the
program needs to handle only one of the events: closing the window.
 
Search WWH ::




Custom Search