Java Reference
In-Depth Information
You could try putting the color selection item in an additional drop-down menu. You can do this by chan-
ging the code that follows the statement adding the separator in the Elements menu as follows:
elementMenu.addSeparator();
JMenu colorMenu = new JMenu("Color"); // Color submenu
elementMenu.add(colorMenu); // Add the submenu
colorMenu.add(redItem = new JCheckBoxMenuItem("Red", false));
colorMenu.add(yellowItem = new JCheckBoxMenuItem("Yellow",
false));
colorMenu.add(greenItem = new JCheckBoxMenuItem("Green", false));
colorMenu.add(blueItem = new JCheckBoxMenuItem("Blue", true));
Directory "Sketcher 3 with Color submenu"
Now you add a JMenu object, colorMenu , to the drop-down menu for Elements. This has its own drop-
down menu consisting of the color menu items. The Color item is displayed on the Elements menu with
an arrow to show that a further drop-down menu is associated with it. If you run the application again
and extend the drop-down menus, the window should be as shown in Figure 17-38 .
FIGURE 17-38
Whether you choose this menu structure or the previous one is a matter of taste. It might even be better
to have a separate item on the menu bar, but let's leave it at that for now. You see in the next chapter that
the programming necessary to deal with menu selections by the user is the same in either case.
Adding Menu Shortcuts and Accelerators
A shortcut is a unique key combination used to select a menu on the menu bar directly from the keyboard to
display the drop-down menu. A typical shortcut under Microsoft Windows would be the Alt key plus a letter
from the menu item label, so the shortcut for the File menu item might be Alt+F. When you enter this key
 
 
Search WWH ::




Custom Search