Java Reference
In-Depth Information
CHAPTER 6
■ ■ ■
Swing Menus and Toolbars
M any of the low-level Swing components were covered in the previous two chapters of this
topic. This chapter will delve into Swing's menu-related components. Menus and toolbars help
make your applications more user-friendly by providing visual command options. Users can
avoid the somewhat archaic multiple-key command sequences that are holdovers from programs
such as the early word processor WordStar and the more current emacs programmer's editor.
Although Swing menus do support multiple-key command sequences, the menus (and toolbars)
are designed primarily for on-screen graphical selection with a mouse, rather than the keyboard.
The menu components discussed in this chapter are used as follows:
For each cascading menu, you create a JMenu component and add it to the JMenuBar .
For the selections available from the JMenu , you create JMenuItem components and add
them to the JMenu .
To create submenus, you add a new JMenu to a JMenu and place JMenuItem options on the
new menu.
Then, when a JMenu is selected, the system displays its current set of components within
a JPopupMenu .
In addition to the basic JMenuItem elements, this chapter covers other menu items, such as
JCheckBoxMenuItem and JRadioButtonMenuItem , which you can place within a JMenu . You'll also
explore the JSeparator class, which serves to divide menu items into logical groups. You'll find
out how to use the JPopupMenu class for general support of pop-up menus that appear after a
JMenu is selected, or in context for any component. As with abstract buttons (the AbstractButton
class was introduced in Chapter 4), each menu element can have a mnemonic associated with
it for keyboard selection. You'll also learn about the support for keyboard accelerators, which
allow users to avoid going through all the menuing levels for selection.
Besides the individual menu-related components, in this chapter you'll look at the JMenuBar
selection model and event-related classes specific to menus. The selection model interface
to examine is the SingleSelectionModel interface, as well as its default implementation
DefaultSingleSelectionModel . You'll explore the menu-specific listeners and events
MenuListener / MenuEvent , MenuKeyListener / MenuKeyEvent , and MenuDragMouseListener /
MenuDragMouseEvent . In addition, you'll examine creating other pop-up components with
Popup and PopupFactory , as well as using toolbars with the JToolBar class.
151
Search WWH ::




Custom Search