Java Reference
In-Depth Information
public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
private static SketchFrame window; // The application window
private static Sketcher theApp; // The application object
}
If you run the Sketcher program again, you will see it works just as before, but now the
Sketcher
class
object is handling the close operation.
How It Works
The
import
statements for the package
java.awt.event
that we have added to the source file ARE
essential here because we need access to the
WindowListener
interface. The
Sketcher
class
implements the
WindowListener
interface, so an object of type
Sketcher
can handle window
events. The method
main()
now creates a
Sketcher
object and calls the
init()
method that we
have added to the class definition to initialize it. The
init()
method does what
main()
did in the
previous version of Sketcher. It also calls the
addWindowListener()
method for the window object.
The argument to
addWindowListener()
is the listener object that is to receive window events. Here
it is the variable
this
- which refers to the application object. If we had other listener objects that we
wanted to register to receive this event, we would just need to add more calls to the
addWindowListener()
method - one call for each listener.
To implement the
WindowListener
interface in the
Sketcher
class, we must implement all seven
methods that are declared in the interface. If we don't do this, the class would be abstract and we could
not create an object of type
Sketcher
. Only the
windowClosing()
method has any code here - all
the rest are empty because we don't need to use them. The
windowClosing()
method does the same
as the
processWindowEvent()
method that we implemented for the previous version of the
SketchFrame
class, but here we don't need to check the object passed to it as this method is only
called for a
WINDOW
_
CLOSING
event. We don't need to pass the event on either: this is only necessary
when you handle events in the manner we discussed earlier. Here, if there were other listeners around
for our window events they would automatically receive the event.
We have included the code that calls
dispose()
and
exit()
here, but if we have set the default close
operation in
SketchFrame
to
EXIT
_
ON
_
CLOSE
, we could omit these too. We really only need to put
our application clean-up code in the
windowClosing()
method, and this will just be prompting the
user to save any application data. We will get to that eventually.
Having to implement six methods that we don't need is rather tedious. But we have a way to get around
this though - by using what is called an
adapter class
, to define a listener.
Using Adapter Classes
An
adapter class
is a term for a class that implements a listener interface with methods that have no content,
so they do nothing. The idea of this is to enable you to derive your own listener class from any of the adapter
classes that are provided, and then implement just the methods that you are interested in. The other empty
methods will be inherited from the adapter class so you don't have to worry about them.
There's an adapter class defined in the
javax.swing.event
package that defines the methods for the
MouseInputListener
interface. There are five further adapter classes defined in the
java.awt.event
package that cover the methods in the other low-level listener interfaces we have seen: