
Home
|


JAVA APPLET
Menu Bar and Menus
The event handling for menus does
seem to benefit from the Java 1.1 event model, but Java’s approach
to menus is still messy and requires a lot of hand coding. The right
medium for a menu seems to be a resource rather than a lot of code. Keep
in mind that program-building tools will generally handle the creation of
menus for you, so that will reduce the pain somewhat (as long as they will
also handle the maintenance!).
In addition, you’ll find the events for
menus are inconsistent and can lead to confusion: MenuItems use ActionListeners,
but CheckboxMenuItems use ItemListeners. The Menu
objects can also support ActionListeners, but that’s not usually
helpful. In general, you’ll attach listeners to each MenuItem or CheckboxMenuItem,
but the following example (revised from the earlier version) also shows
ways to combine the capture of multiple menu components into a single
listener class. As you’ll see, it’s probably not worth the hassle to
do this.
Here is an example code to create
a menu


 |