Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Implements | |
Package | com.apple.cocoa.application |
Companion guide |
The NSMenuItem class defines objects that are used as command items in menus. Additionally, the NSMenuItem class also includes some private functionality needed to maintain binary compatibility with other components of Cocoa. Because of this fact, you cannot replace the NSMenuItem class with a different class. You may, however, subclass NSMenuItem if necessary.
setImage
image
setOnStateImage
onStateImage
setOffStateImage
offStateImage
setMixedStateImage
mixedStateImage
Creates a new menu item with no action and key equivalent and the title "NSMenuItem"
.
public NSMenuItem
()
Creates a new NSMenuItem
.
public NSMenuItem
(String itemName, NSSelector action, String charCode)
The arguments itemName and charCode must not be null
(if there is no title or key equivalent, specify an empty string). The action argument must be a valid selector or null
. For instances of the NSMenuItem
class, the default initial state is NSCell.OffState
, the default on-state image is a checkmark, and the default mixed-state image is a dash.
Returns a menu item that is used to separate logical groups of menu commands.
public static _NSObsoleteMenuItemProtocol protocolSeparatorItem
()
This menu item is disabled. The default separator item is blank space.
If flag is true
, menu items conform to user preferences for key equivalents; otherwise, the key equivalents originally assigned to the menu items are used.
public static void setUsesUserKeyEquivalents
(boolean flag)
Returns true
if menu items conform to user preferences for key equivalents; otherwise, returns false
.
public static boolean usesUserKeyEquivalents
()
Returns the receiver’s action method.
public NSSelector action
()
Returns the custom title string for a menu item.
public NSAttributedString attributedTitle
()
Returns true
if the receiver has a submenu, false
if it doesn’t.
public boolean hasSubmenu
()
setSubmenuForItem
(NSMenu)Returns the image displayed by the receiver, or null
if it displays no image.
public NSImage image
()
Returns the menu item indentation level for the receiver.
public int indentationLevel
()
The return value is from 0 to 15. The default indentation level is 0.
Returns whether the receiver is an alternate to the previous menu item.
public boolean isAlternate
()
Returns true
if the receiver is enabled, false
if not.
public boolean isEnabled
()
Returns whether the receiver is a separator item (that is, a menu item used to visually segregate related menu items).
public boolean isSeparatorItem
()
Returns the receiver’s unmodified keyboard equivalent, or the empty string if one hasn’t been defined.
public String keyEquivalent
()
Use keyEquivalentModifierMask
to determine the modifier mask for the key equivalent.
Returns the receiver’s keyboard equivalent modifier mask.
public int keyEquivalentModifierMask
()
Returns the menu to which the receiver belongs, or null
if no menu has been set.
public NSMenu menu
()
Returns the image used to depict a “mixed state.”
public NSImage mixedStateImage
()
A mixed state is useful for indicating “off” and “on” attribute values in a group of selected objects, such as a selection of text containing boldface and plain (nonboldface) words. By default this is a horizontal line.
Deprecated. Returns the character in the menu item title that appears underlined for use as a mnemonic.
public String mnemonic
()
If there is no mnemonic character, returns an empty string.
Deprecated. Returns the position of the underlined character in the menu item title used as a mnemonic.
public int mnemonicLocation
()
The position is the 0 based index of that character in the title string. If the receiver has no mnemonic character, returns NSArray.NotFound
.
Returns the image used to depict the receiver’s “off” state, or null
if the image has not been set.
public NSImage offStateImage
()
By default there is no image.
Returns the image used to depict the receiver’s “on” state, or null
if the image has not been set.
public NSImage onStateImage
()
By default this image is a checkmark.
Returns the object that the receiving menu item represents.
public Object representedObject
()
For example, you might have a menu list the names of views that are swapped into the same panel. The represented objects would be the appropriate NSView
objects. The user would then be able to switch back and forth between the different views that are displayed by selecting the various menu items.
Returns a menu item that is used to separate logical groups of menu commands.
public NSMenuItem separatorItem
()
This menu item is disabled. The default separator item is blank space.
Sets the receiver’s action method to aSelector.
public void setAction
(NSSelector aSelector)
See Action Messages for additional information on action messages.
Marks the receiver as an alternate to the previous menu item.
public void setAlternate
(boolean isAlternate)
If the receiver has the same key equivalent as the previous item, but has different key equivalent modifiers, the items are folded into a single visible item and the appropriate item shows while tracking the menu. The menu items may also have no key equivalent as long as the key equivalent modifiers are different.
If there are two or more items with no key equivalent but different modifiers, then the only way to get access to the alternate items is with the mouse. If you mark items as alternates but their key equivalents don’t match, they might be displayed as separate items. Marking the first item as an alternate has no effect.
The isAlternate value is archived.
Specifies a custom string for a menu item.
public void setAttributedTitle
(NSAttributedString string)
You can use this method to add styled text and embedded images to menu item strings. If you do not set a text color for the attributed string, it is black when not selected, white when selected, and gray when disabled. Colored text remains unchanged when selected.
When you call this method to set the menu title to an attributed string, the setTitle
method is also called to set the menu title with a plain string. If you clear the attributed title, the plain title remains unchanged. To clear the attributed title, set the attributed string to either null
or an empty attributed string.
The attributed string is not archived in the old nib format.
Sets whether the receiver is enabled based on flag.
public void setEnabled
(boolean flag)
If a menu item is disabled, its keyboard equivalent is also disabled. See the “NSMenu.MenuValidation” interface specification for cautions regarding this method.
Sets the receiver’s image to menuImage.
public void setImage
(NSImage menuImage)
If menuImage is null
, the current image (if any) is removed. This image is not affected by changes in menu-item state.
Sets the menu item indentation level for the receiver.
public void setIndentationLevel
(int indentationLevel)
The value for indentationLevel may be from 0 to 15. If indentationLevel is greater than 15, the value is pinned to the maximum. If indentationLevel is less than 0, an exception is thrown. The default indentation level is 0.
The indentationLevel value is archived.
Sets the receiver’s unmodified key equivalent to aString.
public void setKeyEquivalent
(String aString)
If you want to remove the key equivalent from a menu item, pass an empty string (""
) for aString (never pass null
). Use setKeyEquivalentModifierMask
to set the appropriate mask for the modifier keys for the key equivalent.
If you want to specify the Backspace key as the key equivalent for a menu item, use a single character string with NSBackspaceCharacter (defined in NSText.h as 0x08
) and for the Forward Delete key, use NSDeleteCharacter (defined in NSText.h as 0x7F
). Note that these are not the same characters you get from an NSEvent key-down event when pressing those keys.
Sets the receiver’s keyboard equivalent modifiers (indicating modifiers such as the Shift or Option keys) to those in mask.
public void setKeyEquivalentModifierMask
(int mask)
mask is an integer bit field containing any of these modifier key masks, combined using the C bitwise OR operator:
NSEvent.ShiftKeyMask
NSEvent.AlternateKeyMask
NSEvent.CommandKeyMask
NSEvent.ControlKeyMask
You should always set NSEvent.CommandKeyMask
in mask.
NSEvent.ShiftKeyMask
is relevant only for function keys—that is, for key events whose modifier flags include NSEvent.FunctionKeyMask
. For all other key events NSEvent.ShiftKeyMask
is ignored, and characters typed while the Shift key is pressed are interpreted as the shifted versions of those characters; for example, Command-Shift-c is interpreted as Command-C.
See the NSEvent class specification for more information about modifier mask values.
Sets the receiver’s menu to aMenu.
public void setMenu
(NSMenu aMenu)
This method is invoked by the owning NSMenu when the receiver is added or removed. You shouldn’t have to invoke this method in your own code, although it can be overridden to provide specialized behavior.
Sets the image of the receiver that indicates a “mixed” state, that is, a state neither “on” nor “off.”
public void setMixedStateImage
(NSImage itemImage)
If itemImage is null
, any current mixed-state image is removed. Changing state images is currently unsupported in Mac OS X.
Deprecated. Sets the character of the menu item title at location that is to be underlined.
public void setMnemonicLocation
(int location)
location must be from 0 to 254. This character identifies the access key by which users can access the menu item.
Sets the image of the receiver that indicates an “off” state.
public void setOffStateImage
(NSImage itemImage)
If itemImage is null
, any current off-state image is removed. Changing state images is currently unsupported in Mac OS X.
Sets the image of the receiver that indicates an “on” state.
public void setOnStateImage
(NSImage itemImage)
If itemImage is null
, any current on-state image is removed. Changing state images is currently unsupported in Mac OS X.
Sets the object represented by the receiver to anObject.
public void setRepresentedObject
(Object anObject)
By setting a represented object for a menu item, you make an association between the menu item and that object. The represented object functions as a more specific form of tag that allows you to associate any object, not just an int, with the items in a menu.
For example, an NSView object might be associated with a menu item—when the user chooses the menu item, the represented object is fetched and displayed in a panel. Several menu items might control the display of multiple views in the same panel.
Sets the state of the receiver to itemState, which should be one of NSCell.OffState
, NSCell.OnState
, or NSCell.MixedState
.
public void setState
(int itemState)
The image associated with the new state is displayed to the left of the menu item.
Sets the submenu of the receiver to aSubmenu.
public void setSubmenu
(NSMenu aSubmenu)
The default implementation of the NSMenuItem
class throws an exception if aSubmenu already has a supermenu.
Sets the receiver’s tag to anInt.
public void setTag
(int anInt)
Sets the receiver’s target to anObject.
public void setTarget
(Object anObject)
Sets the receiver’s title to aString.
public void setTitle
(String aString)
Deprecated. Sets the title of a menu item with a character denoting an access key.
public void setTitleWithMnemonic
(String aString)
Use an ampersand character to mark the character (the one following the ampersand) to be designated.
Sets a help tag for a menu item.
public void setToolTip
(String toolTip)
You can call this method for any menu item, including items in the main menu bar.
This string is not archived in the old nib format.
Returns the state of the receiver, which is NSCell.OffState
(the default), NSCell.OnState
, or NSCell.MixedState
.
public int state
()
Returns the submenu associated with the receiving menu item, or null
if no submenu is associated with it.
public NSMenu submenu
()
If the receiver responds true
to hasSubmenu
, the submenu is returned.
Returns the receiver’s tag.
public int tag
()
Returns the receiver’s target.
public Object target
()
Returns the receiver’s title.
public String title
()
Returns the help tag for a menu item.
public String toolTip
()
Returns the user-assigned key equivalent for the receiver.
public String userKeyEquivalent
()
Returns the modifier mask for the receiver’s user-assigned key equivalent.
public int userKeyEquivalentModifierMask
()
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-01)