Java Reference
In-Depth Information
Display 18.2
A Window Listener
(part 3 of 3)
R
ESULTING
GUI
This window is an object of
the class
ConfirmWindow
.
When you click this close-
window button, the second
window appears.
Are you sure you want to exit?
I like to be sure you are sincere.
The
dispose
Method
The method
dispose
is a method in the class
JFrame
that releases any resources used
by the
JFrame
or any of its components. So, a call to
dispose
eliminates the
JFrame
and its components, but if the program has items that are not components of the
JFrame,
then the program does not end. For example, in Display 18.2, the smaller
window of the class
ConfirmWindow
invokes
dispose
(if the user clicks the
"No"
button). That causes that smaller window to go away, but the larger window remains.
dispose
The
dispose
Method
The class
JFrame
has a method named
dispose
that will eliminate the invoking
JFrame
without ending the program. When
dispose
is invoked, the resources consumed by
the
JFrame
and its components are returned for reuse, so the
JFrame
is gone, but the
program does not end (unless
dispose
eliminates all elements in the program, as in a one-
window program). The method
dispose
is often used in a program with multiple windows
to eliminate one window without ending the program.
SYNTAX
JFrame_Object
.dispose();
The
JFrame_Object
is often an implicit
this
. A complete example of using
dispose
can
be seen in Display 18.2.