Java Reference
In-Depth Information
I changed the name of the
createWindow()
method to
createGUI()
because this reflects better what
it does. It is now an instance method. It is called on the event-dispatching thread using the application
object,
theApp
. The
createGUI()
method creates the application window object as before, but now the
reference is stored in a field that is called
theApp
.
After setting up the window components, the
createGUI()
method calls the
addWindowListener()
method for the window object. The argument to the
addWindowListener()
method is a reference to the
listener object that is to receive window events. Here it is the variable
this
, which refers to the applica-
tion object,
theApp
. If you had other listener objects that you wanted to register to receive this event, you
would just need to add more calls to the
addWindowListener()
method — one call for each listener.
When you implement the
WindowListener
interface in the
Sketcher
class, you must implement all sev-
en methods that are declared in the interface. If you failed to do this, the class would be abstract and you
could not create an object of type
Sketcher
. Only the
windowClosing()
method contains code here —
the bodies of all the other methods are empty because you don't need to use them. The
windowClosing()
method does the same as the
processWindowEvent()
method that you implemented for the previous
version of the
SketcherFrame
class, but here you don't need to check the object passed to it because the
windowClosing()
method is called only for a
WINDOW_CLOSING
event. You don't need to pass the event
on either; this is necessary only when you handle events in the manner I discussed earlier. Here, if there
were other listeners around for the window events, they would automatically receive the event.
You have included the code that calls
dispose()
and
exit()
here, but if you set the default close oper-
ation in
SketcherFrame
to
EXIT_ON_CLOSE
, you could omit these, too. You really only need to put your
application cleanup code in the
windowClosing()
method, and this typically displays a dialog to just
prompt the user to save any application data. You get to that eventually.
Having to implement six methods that you don't need is rather tedious. But you have a way to avoid this
— 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 are inherited from the adapter class so you don't have to worry about them.
The
MouseInputAdapter
adapter class that is defined in the
javax.swing.event
package 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 you have seen.
Here's the complete set:
FocusAdapter
WindowAdapter
KeyAdapter
MouseAdapter
MouseMotionAdapter
MouseInputAdapter
The
WindowAdapter
class implements all the methods declared in the
WindowListener
,
WindowFo-
cusListener
, and
WindowStateListener
interfaces. The other five adapter classes each implement the
methods in the corresponding listener interface.
To handle the window closing event for the
Sketcher
application, you could derive your own class from
the
WindowAdapter
class and just implement the
windowClosing()
method. If you also make it a nested
class to the
Sketcher
class, it automatically has access to the members of the
Sketcher
object regardless of