Dynamically Modifying Menus

So far, we have modified menus before they are created, by adding items to them. Sometimes it is necessary to modify a menu after it is created.

The SelfModifyingMenu illustrated in Figure 17-7 contains an item that which is either enabled or and disabled by the menu item above it. SelfModifyingMenu also contains two menu items for adding an item and removing the last item added to the menu.

Figure 17-7. A Self-Modifying Menu. The top pair of pictures show the “Enable/Disable Me” item grayed out. The bottom pair of pictures show a menu item added to the menu. Tear-off menus are used to show the different states of the self-modifying menu.

Example 17-10 lists the SelfModifyingMenu class.

Example 17-10. SelfModifyingMenu ...

Get Graphic Java™ 1.2, Volume I: AWT, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.