ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button O'Reilly Book Excerpts: Java Swing, 2nd Edition

Java Swing: Menus and Toolbars, Part 5

by Robert Eckstein, Marc Loy, Dave Wood, James Elliott, Brian Cole

In part five in this book excerpt series on Swing menus and toolbars from Java Swing, 2nd Edition, learn how to attach menus to menu bars with the JMenu class.

The JMenu Class

The JMenu class represents the anchored menus attached to a JMenuBar or another JMenu. Menus directly attached to a menu bar are called top-level menus. Submenus, on the other hand, are not attached to a menu bar but to a menu item that serves as its title. This menu item title is typically marked by a right arrow, indicating that its menu appears alongside the menu item if the user selects it. See Figure 14-11.

Figure 14-11. Top-level menu and submenu

Related Reading

Java Swing
By Marc Loy, Robert Eckstein, Dave Wood, James Elliott, Brian Cole

JMenu is a curious class. It contains a MenuUI delegate, but it uses a ButtonModel for its data model. To see why this is the case, it helps to visualize a menu as two components: a menu item and a pop-up menu. The menu item serves as the title. When it is pressed, it signals the pop-up menu to show itself either below or directly to the right of the menu item. JMenu actually extends the JMenuItem class, which makes it possible to implement the title portion of the menu. This, in effect, makes it a specialized button. On some platforms you can use the mnemonic property of the JMenuItem superclass to define a shortcut for the menu's title and, consequently, the menu. In addition, you can use the enabled property of JMenuItem to disable the menu if desired.

As with pop-up menus, you can add or insert JMenuItem, Component, or Action objects in the pop-up portion of the menu by calling the add( ) and insert( ) methods. You can also add a simple string to the menu; JMenu creates the corresponding JMenuItem object for you internally. The JMenu class assigns an integer index to each menu item and orders them based on the layout manager used for the menu. You can also add separators to the menu by using the addSeparator( ) method.

Warning: You cannot use keyboard accelerators with JMenu objects (top-level or submenu), because accelerators trigger actual program actions, not simply the display of a menu from which actions can be chosen. On some platforms you can use the setMnemonic( ) method to set a shortcut to bring up the menu, but the only universal, reliable approach is to assign keyboard accelerators to the non-submenu JMenuItems that trigger program actions.

You can programmatically cause the submenu to pop up on the screen by setting the popupMenuVisible property to true. Be aware that the pop up does not appear if the menu's title button is not showing.

Figure 14-12 shows the class diagram for the JMenu component.

Figure 14-12. JMenu class diagram

Properties

The JMenu properties are listed in Table 14-8. JMenu uses a JPopupMenu to represent its list of menu items. If you wish to access that underlying menu, you can do so using the popupMenu property. The popupMenuVisible property tracks whether the menu's pop-up portion is currently visible. As noted, setting this to true when the title button is visible causes the pop up to appear. JMenu also contains a selected property, which indicates if the user has selected the title button of the menu. Both properties should mirror each other.

Table 14-8: JMenu properties

Property

Data type

get

is

set

Default value

accessibleContexto

Accessible Context

·

 

 

JMenu.accessibleJMenu( )

component

Component

·

 

 

 

componentOrientation1.4, o

ComponentOrientation

·

 

·

From L&F

delay

int

·

 

·

0

itemCount

int

·

 

 

0

itemi

JMenuItem

·

 

 

null

layouto

LayoutManager

·

 

·

OverlayLayout( )

menuComponentCount

int

·

 

 

0

menuComponenti

Component

·

 

 

null

menuComponents

Component[ ]

·

 

 

 

menuListeners1.4

MenuListener[ ]

·

 

 

 

modelo

ButtonModel

·

 

·

DefaultButtonModel( )

popupMenu

JPopupMenu

·

 

 

 

popupMenuVisible

boolean

 

· ·

false

selected

boolean

 

· ·

false

subElements

MenuElement[ ]

·

 

 

 

tearOffu

boolean

 

·

 

Throws an Error

topLevelMenub, o

boolean

 

·

 

 

UIb

MenuUI

 

 

·

From L&F

UIClassID

String

·

 

 

"MenuUI"

1.4since 1.4, bbound, iindexed, ooverridden, uunimplemented
See also properties from the JMenuItem class (Table 14-4).

The topLevelMenu property has the value true if this JMenu is directly attached to a menu bar and is not a submenu. item is an indexed property that allows access to each of the JMenuItem objects in the menu, while itemCount maintains a count of all of the JMenuItem objects that are present. The delay property specifies the amount of time, in milliseconds, that the underlying menu waits to appear or disappear after receiving the corresponding event. The delay must be set to a positive integer, or setDelay( ) throws an IllegalArgumentException.

The menuComponent property is a more generalized version of the item property; it returns the component at the given index as a Component rather than as a JMenuItem. In addition, the menuComponentCount property retains a count of the menu items, separators, and other components currently in the menu. The menuComponents property lets you access each of the items in the menu, returned as an array of Component objects.

The componentOrientation property is used to accommodate non-Western languages in which text does not flow left to right. JMenu overrides this property in order to properly pass changes on to the JPopupMenu delegate it uses.

Warning: The tearOff property is not yet implemented and is reserved for (increasingly dubious) future use in Swing. Trying to use it throws an Error (rather than something more appropriate like an UnsupportedOp-erationException). Since an Error is supposed to indicate a catastrophic failure of the virtual machine, using this property will almost certainly crash your application.

Constructor

public JMenu(  )
public JMenu(Action a)
public JMenu(String s)
public JMenu(String s, boolean b) 

Initialize a default JMenu. You have the option of specifying a string for the JMenu to display--as well as a boolean for the tearOff property (which is ignored)--or binding it to an Action.

Pages: 1, 2

Next Pagearrow