Java Reference
In-Depth Information
EXAMPLE:
A Window Listener Inner Class
Display 18.2 gives an example of a
JFrame
class with a window listener class that is
an inner class. The window listener inner class is named
CheckOnExit.
A window
listener class need not be an inner class, but it is frequently convenient to make a
window listener class (or other kind of listener class) an inner class.
The main
JFrame
in Display 18.2 simply displays a message. What is interesting
is how the window listener programs the close-window button. You can apply
the window listener used in this
JFrame
to any
JFrame.
When the close-window
button is clicked, a second, smaller window appears and asks
"Are you sure you
want to exit?"
If the user clicks the
"Yes"
button, the entire program ends
and so both windows go away. If the user clicks the
"No"
button, only the smaller
window disappears; the program and the main window continue. Let us look at the
programming details.
When the close-window button in the main window is clicked, this fires a window
event. The only registered window listener is the anonymous object that is the
argument to
addWindowListener.
Next we repeat the relevant line of code, which is
in the constructor for
WindowListenerDemo:
addWindowListener(
new
CheckOnExit());
This anonymous window listener object receives the window event fired when
the close-window button is clicked and then invokes the method
windowClosing.
The method
windowClosing
creates and displays a window object of the class
ConfirmWindow,
which contains the message
"Are you sure you want to
exit?"
as well as the two buttons labeled
"Yes"
and
"No".
If the user clicks the
"Yes"
button, the action event fired by that button goes
to the
actionPerformed
method, which ends the program with a call to
System.
exit.
If the user clicks the
"No"
button, then the
actionPerformed
method invokes
the method
dispose.
The method
dispose,
discussed in the next subsection,
makes its calling object go away but does not end the program. The calling object for
dispose
is the smaller window (which is an object of the class
ConfirmWindow
), so
this smaller window goes away but the main window remains.
Notice that even though we have registered a window listener, which says what should
happen when the close-window button is clicked, we still need to invoke the method
setDefaultCloseOperation.
When the close-window button is clicked, the policy
set by
setDefaultCloseOperation
is always carried out in addition to any action by
window listeners. If we do not include any invocation of
setDefaultCloseOperation,
then the default action is to make the window go away (but not to end the program).
We do not want our main window to go away, so we set the policy as follows:
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
This means that clicking the close-window button causes no action other
than the actions of any window listeners. If you are using a window listener to set
the action of the close-window button, you invariably want an invocation of
setDefaultCloseOperation
with the argument
JFrame.DO_NOTHING_ON_CLOSE.