Java Reference
In-Depth Information
EXAMPLE:
A Window Listener Inner Class
Display 18.2 gives an example of a
class with a window listener class that is an inner
class. The window listener inner class is named
JFrame
. 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
CheckOnExit
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
. When the close-window button is clicked, a second, smaller
JFrame
JFrame
window appears and asks:
If the user clicks the
"Are you sure you want to exit?"
"Yes"
button, the entire program ends and so both windows go away. If the user clicks the
but-
ton, only the smaller window disappears; the program and the main window continue. Let's
look at the programming details.
When the close-window button in the main window is clicked, that fires a window event.
The only registered window listener is the anonymous object that is the argument to
"No"
. Below we repeat the relevant line of code, which is in the constructor for
addWindowListener
:
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
. The method
cre-
windowClosing
windowClosing
ates and displays a window object of the class
, which contains the message
ConfirmWindow
"Are
as well as the two buttons labeled
and
.
you sure you want to exit?"
"Yes"
"No"
"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 hap-
pen 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:
If the user clicks the
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
.