Properties

The properties for the JMenuItem class are shown in Table 14-4. Most of the properties shown are superclass properties reconfigured to ensure that the menu item's "button" acts like a menu item should. The borderPainted property is always false; menu items never take a border. The focusPainted property is also false to ensure that a focus rectangle is never drawn around the menu item. horizontalTextPosition and horizontalAlignment are both initialized to JButton.LEFT. This places the text to the left of the image icon and places the text and image icon on the left side of the menu item. (See the previous example for information on how to reconfigure this.)

Table 14-4: JMenuItem properties

Property

Data type

get

is

set

Default value

acceleratorb

KeyStroke

·

·

null

accessibleContexto

Accessible Context

·

JMenuItem.AccessibleJMenuItem( )

armedb, o

boolean

·

·

false

borderPaintedo

boolean

·

·

false

componento

Component

·

enabledo

boolean

·

·

true

focusPaintedo

boolean

·

·

false

horizontalAlignmento

int

·

·

JButton.LEFT

horizontalTextPositiono

int

·

·

JButton.LEFT

menuDragMouseListeners1.4

MenuDragMouseListener[ ]

·

menuKeyListeners1.4

MenuKeyListener[ ]

·

modelo

ButtonModel

·

·

DefaultButtonModel( )

subElementso

MenuElement[ ]

·

UIb

MenuItemUI

·

From L&F

UIClassIDo

String

·

"MenuItemUI"

1.4since 1.4, bbound, ooverridden
See also properties from the AbstractButton class (Table 5-4).

The accelerator property sets the keyboard accelerator for the menu item; the accelerator is typically drawn to the right of the menu item string. The armed property simply maps a boolean down to the armed state of the component model, ButtonModel. You can use this to programmatically select the menu item, if needed. The enabled property is a boolean that indicates whether the user can select the menu item. If the menu item is disabled, JMenuItem automatically grays the text and associated image. As discussed earlier, the most powerful way to control the enabled state of a menu item is to associate it with an Action object so that it automatically tracks the action's enabled state. The subElements property provides an array of submenus contained in this menu item.

Constructors

Create a menu item with the appropriate icon or string. You also have the option to specify a mnemonic if you initialize with a string. Since Version 1.3, you can use the properties of an Action to directly configure the properties of the JMenuItem.

Events

JMenuItems send many different kinds of events. Perhaps the most important are ActionEvents, which are fired when an item is selected. ChangeEvents are fired when button properties change. Methods for adding and removing listeners for these events are inherited from AbstractButton.

JMenuItem also uses special events for reporting mouse motions and key presses on top of the menu item. These are the MenuDragMouseEvent and MenuKeyEvent. Here are the methods for registering listeners for these events:

These methods add or remove a specific MenuKeyListener interested in being notified when there is a MenuKeyEvent.

The following methods provide support for firing these events, though you will probably never need to call them:

public void processMenuDragMouseEvent (MenuDragMouseEvent e)

Fire a specific MenuDragMouseEvent notification based on the type of MouseEvent that was observed. If the MouseEvent listed was MOUSE_ENTERED, for example, the menu invokes the fireMenuDragMouseEntered( ) method.

public void processMenuKeyEvent (MenuKeyEvent e)

Fire a specific MenuKeyEvent notification based on the type of MenuKeyEvent that was observed. If the MenuKeyEvent listed was KEY_RELEASED, for example, the menu invokes the fireMenuKeyReleased( ) method.

Method

public void updateUI( )

Force the current UI manager to reset the current delegate for the component, thus updating the component's L&F.