Java Reference
In-Depth Information
will invoke the openFile method of its enclosing class when activated. Each subtype of
ActionListener created in this way represents a unique anonymous class.
Just like named inner classes, anonymous inner classes are able to access the fields and meth-
ods of their enclosing classes. In addition, because they are defined inside a method, they are
able to access the local variables and parameters of that method. However, an important rule
is that local variables accessed in this way must be declared as final variables. You will see an
example of this in the imageviewer2-0 project, discussed in Section 11.6.
It is worth emphasizing some observations about anonymous inner classes. First, for our con-
crete problem, using anonymous inner classes is very useful. It allows us to completely remove
the central actionPerformed method from our ImageViewer class. Instead, we create a sep-
arate, custom-made action listener (class and object) for each menu item. This action listener
can directly call the method implementing the corresponding function.
This structure is nicely cohesive and extendable. If we need an additional menu item, we just
add code to create the item and its listener, as well as the method that handles its function. No
listing in a central method is required.
Second, using anonymous inner classes can make code quite hard to read. It is strongly recom-
mended to use them only for very short classes and for well-established code idioms. For us,
implementing event listeners is the only example in this topic where we use this construct. 4
Anonymous inner
classes are a
useful construct for
implementing event
Third, we often use anonymous classes where only a single instance of the implementation will
be required—where the actions associated with each menu item are unique to that particular
item. In addition, the instance will always be referred to via its supertype. Both reasons mean
there is less need for a name for the new class; hence, it can be anonymous.
For all our following work, we shall avoid the central actionPerformed method and use
anonymous inner classes instead. So you should leave the imageviewer0-2 project behind and
use the structure from imageviewer0-3 as a basis for your further work.
Summary of key GUI elements
At the beginning of this chapter, we listed the three areas of GUI construction: components,
layout, and event handling. So far, we have concentrated on two of these areas. We have en-
countered a small number of components (labels, buttons, menus, menu items), and we have
discussed the handling of action events in quite some detail.
Getting to the current state (showing a frame with a label and a few menus) was hard work,
and we had to discuss a lot of background concepts. It gets easier from now on—really!
Understanding the event handling for menu items was probably the most difficult detail we had
to master for our example.
Adding more menus and other components to the frame will now be quite easy—just more
of the same. The one entirely new area we shall have to look at is layout : how to arrange the
components in the frame.
If you'd like to find out more about inner classes, have a look at these two sections of the online Java tuto-
rial: and .
Search WWH ::

Custom Search