14.2 Menu Bar Selection Models
In all GUI environments, menu
14.2.1 The SingleSelectionModel Interface
Objects implementing the
SingleSelectionModel
interface do exactly what its
14.2.1.1 Properties
Objects implementing the
SingleSelectionModel
interface contain the properties shown in Table 14-1. The
selected
property is a
boolean
that
Table 14-1. SingleSelectionModel properties
14.2.1.2 Events
Objects implementing the
SingleSelectionModel
interface must fire a
ChangeEvent
(not a
PropertyChangeEvent
) when the object modifies its
selectedIndex
property, i.e., when the selection has changed. The interface contains the standard
addChangeListener( )
and
removeChangeListener( )
14.2.1.3 MethodThe SingleSelectionModel interface contains one other method:
14.2.2 The DefaultSingleSelectionModel ClassSwing provides a simple default implementation of the SingleSelectionModel interface in the DefaultSingleSelectionModel class. 14.2.2.1 Properties
DefaultSingleSelectionModel
contains just the properties required by the
SingleSelectionModel
interface, as shown in Table 14-2. The
selectedIndex
property is an integer index that represents the currently selected item. The default value of
-1
indicates that there is no selection. The
selected
property is a
boolean
that returns
true
if the
selectedIndex
is anything other than
-1
, and
false
Table 14-2. DefaultSingleSelectionModel properties
14.2.2.2 Events and methodsThe DefaultSingleSelectionModel object provides all the events and methods specified by the SingleSelectionModel interface discussed earlier. |
14.3 The JMenuBar Class
Swing's
JMenuBar
class supersedes the AWT
MenuBar
class. This class creates a horizontal menu bar component with zero or more
Figure 14-4. JMenuBar class diagram
You can add
JMenu
objects to the menu bar with the
add( )
method of the
JMenuBar
class.
JMenuBar
then
14.3.1 Menu Bar Placement
You can attach menu bars to Swing
JFrame frame = new JFrame("Menu");
JMenuBar menuBar = new JMenuBar( );
// Attach the menu bar to the frame.
frame.setJMenuBar(menuBar);
The
setJMenuBar( )
method is analogous to the
setMenuBar( )
method of
java.awt.Frame
. Like its predecessor,
setJMenuBar( )
allows the L&F to determine the location of the menu (typically, it anchors the menu bar to the top of a frame, adjusting the frame's internal
Insets
If your application is running on a Macintosh, the Mac L&F can be configured to place menu bars at the top of the screen, where Mac users expect to find them. Setting the system property
com.apple.
The second way to add a menu bar is much less common. Recall that the
JMenuBar
class extends
JComponent
. This means it can be positioned by a Swing layout manager like other Swing
menuBar.setBorder(new BevelBorder(BevelBorder.RAISED)); frame.getContentPane( ).add(menuBar, BorderLayout.SOUTH);
This places the menu bar at the bottom of the frame, as shown in Figure 14-5. (Note that we set a beveled border around the menu bar to help outline its location.) It would even be possible to add two or three menu bars in different locations. Swing does not require a single menu bar to be anchored to the top of a frame. Because they extend
JComponent
, multiple menu bars can be positioned
Figure 14-5. JMenuBar positioned as a Swing component
Of course, you'd never actually want to do this without a very compelling reason. It robs the L&F of its opportunity to place the menu bar in the appropriate location. Moving something as fundamental as a menu bar is almost certain to cause confusion and usability challenges for your users; having multiple menu bars would be baffling. 14.3.2 Properties
The properties of the
JMenuBar
class are shown in Table 14-3.
menu
is an indexed property that references each
JMenu
attached to the menu bar. The read-only
menuCount
property maintains a count of these attached menus. Remember that the single selection model allows only one menu to be activated at a time. If any menu is currently activated, the
selected
property returns
true
; otherwise, the property returns
false
. The
componentAtIndex
property
Table 14-3. JMenuBar properties
The
margin
property controls the amount of space between the menu bar's border and its menus while the
borderPainted
property can be used to suppress the painting of the menu bar's border even if the
border
property has a non-
null
value. Setting
borderPainted
to
false
14.3.2.1 Constructor
14.3.2.2 Menu
14.3.2.3 Miscellaneous
JMenuBar
also implements the
|