Java Reference
In-Depth Information
Figure 14-7 . Creating menus
How It Works
Menus provide standard ways to allow users to select options from windowed platform
applications. Menus should also have hot keys or keyboard equivalents. Users will of-
ten want to use the keyboard instead of the mouse to navigate the menu. This recipe
parallels Recipe 14-8, and you'll notice lots of similarities.
To create a menu, first create an instance of a MenuBar that will contain one-to-
many menu ( MenuItem ) objects. Creating a menu bar:
MenuBar menuBar = new MenuBar();
Secondly, create menu ( Menu ) objects that contain one-to-many menu item
( MenuItem ) objects and other Menu objects making submenus. To create a menu:
Menu menu = new Menu("File");
Third, create menu items to be added to Menu objects, such as menu ( MenuItem ),
check ( CheckMenuItem ), and radio menu items ( RadioMenuItem ). Menu items
can have icons in them. We don't showcase this in the recipe, but we encourage you to
explore the various constructors for all menu items ( MenuItem ). When creating a ra-
dio menu item ( RadioMenuItem ), you should be aware of the ToggleGroup
class. The ToggleGroup class is also used on regular radio buttons ( RadioBut-
ton s) to allow only one selected option at any one time. The following code creates
radio menu items ( RadioMenuItem s) to be added to a Menu object:
 
Search WWH ::




Custom Search