Java Reference
In-Depth Information
}
private static SketchFrame window; // The application window
private static Sketcher theApp; // The application object
}
How It Works
As the
Sketcher
class is no longer the listener for
window
, it doesn't need to implement the
WindowListener
interface. The
WindowHandler
class is the listener class for window events.
Because the
WindowHandler
class is an inner class to the
Sketcher
class, it has access to all the
members of the class, so calling the
dispose()
method for the
window
object is still quite
straightforward - we just access the
window
member of the top-level class.
The
WindowAdapter
object that is the listener for the
window
object is created in the argument to the
addWindowListener()
method for
window
. We don't need an explicit variable to contain it. It will
be stored in a data member of the
Window
class object. This data member is inherited from the
Window
superclass for our
SketchFrame
class.
An easy mistake to make when you're using adapter classes is to misspell the name of
the method that you are using to implement the event - typically by using the wrong
case for a letter. In this case, you won't be overriding the adapter class method at all;
you will be adding a new method. Your code will compile perfectly well but your
program will not handle any events. They will all be passed to the method in the
adapter class with the name your method should have had - which does nothing of
course. This can be a bit mystifying until you realize where the problem is.
We haven't finished with low-level events yet by any means and we'll return to handling more low-level
events in the next chapter when we begin to add drawing code to the Sketcher program. In the
meantime, let's start looking at how we can manage semantic events.
Semantic Events
As we saw earlier, semantic events relate to operations on the components in the GUI for your program.
If you select a menu item or click on a button for example, a semantic event is generated. There are
three classes that represent the basic semantic events you will be dealing with most of the time, and they
are derived from the
AWTEvent
class, as shown in the diagram.