Window Closing Examples
We will now deal only with Java 1.1 events, but within this model you can
accomplish the same task in many different ways. In this lesson we will look
at different ways to get a window frame to close. The behavior of all these
examples will be identical, BUT the structure of the program will be
different. So, if nothing more you will learn that
there's more than one way to close a window!
NOTE: Command line applications... see info at bottom of page.
External Class Extending Adapter Class:
All the sample programs presented on this page will close a window. This
one uses a class which external from the class which creates the frame
to accomplish this task. You will notice the line:
   f.addWindowListener(new WA(f));
This line attaches a windowListener to the frame. The windowListener
is implemented by extending the WindowAdapter class. The windowClosing()
method is defined in such a way as to cause the frame window to close
when the X in the upper right hand corner is clicked.
Inner Class Extending Adapter Class:
Here the class which sets up the Window Event handling facilities is a class
inside the class that extends Frame. Again there is an addWindowListener()
method and the WW class looks a lot like the WA class in the first example.
Class Extending Adapter Class:
This class extends WindowAdapter and contains a Frame. You can see the
usual addWindowListener() and windowClosing() method.
Class Implementing Listener Class:
Here the WindowListener interface is implemented. When you implement
an interface all its methods must be defined. You will notice that there
are six methods which are defined with empty curly braces. They aren't used
and so there is no need to define behavior for them.
Using an Anonymous Inner Class:
The most concise way to handle events is with anonymous inner classes. You
can see all the usual methods used here, but everything done with a
minimum of coding.
Low-Level Event Handling:
This last method is quite different from the other five, but it still
gets the job done. Here the enableEvents(), processWindowEvent(), and
getID() methods are used to deal with events.