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 |
This class defines an object that manages an application’s menus.
indexOfItem
indexOfItemWithTitle
indexOfItemWithTag
indexOfItemWithTargetAndAction
indexOfItemWithRepresentedObject
indexOfItemWithSubmenu
setSubmenuForItem
submenuAction
attachedMenu
isAttached
isTornOff
locationForSubmenu
supermenu
setSupermenu
contextMenuRepresentation
setContextMenuRepresentation
tearOffMenuRepresentation
setTearOffMenuRepresentation
menuUpdateItemAtIndex
delegate method
menuNeedsUpdate
delegate method
numberOfItemsInMenu
delegate method
menuHasKeyEquivalent
delegate method
menuKeyEquivalentAction
delegate method
menuKeyEquivalentTarget
delegate method
Creates a new empty menu.
public NSMenu
()
Creates a new menu using aTitle for its title with autoenabling of menu items turned on.
public NSMenu
(String aTitle)
Returns true
if the menu bar is visible, false
otherwise.
public static boolean menuBarVisible
()
Displays menu as a context menu over view for event.
public static void popUpContextMenu
(NSMenu menu, NSEvent event, NSView view)
Displays menu as a context menu over view for event using font.
public static void popUpContextMenu
(NSMenu menu, NSEvent event, NSView view, NSFont font)
If you pass in null
for the font, the method uses the default font for menu.
Sets whether the menu bar is visible and selectable by the user.
public static void setMenuBarVisible
(boolean visible)
Adds the menu item newItem to the end of the receiver.
public void addItem
(NSMenuItem newItem)
The receiver does not accept the menu item if it already belongs to another menu. After adding the menu item, the receiver updates itself.
Adds a new item with title aString, action aSelector, and key equivalent keyEquiv to the end of the receiver.
public NSMenuItem addItem
(String aString, NSSelector aSelector, String keyEquiv)
Returns the new menu item. If you do not want the menu item to have a key equivalent, keyEquiv should be an empty string and not null
.
Returns the menu currently attached to the receiver or null
if there’s no such object.
public NSMenu attachedMenu
()
Returns whether the receiver automatically enables and disables its menu items based on the “NSMenu.MenuValidation” interface.
public boolean autoenablesItems
()
By default NSMenus do autoenable their menu items. See the interface specification for more information.
Deprecated. Returns null
.
public Object contextMenuRepresentation
()
Returns the receiver’s delegate.
public Object delegate
()
Overridden by subclasses to implement specialized context-sensitive help behavior by causing the Help manager to display the help associated with the receiver.
public void helpRequested
(NSEvent event)
Never invoke this method directly.
showContextHelpForObject
(NSHelpManager)Returns the index identifying the location of menu item anObject in the receiver.
public int indexOfItem
(NSMenuItem anObject)
If no such menu item is in the menu, returns –1.
Returns the index of the first menu item in the receiver that has anObject as its represented object.
public int indexOfItemWithRepresentedObject
(Object anObject)
If no such menu item is in the menu, returns –1.
Returns the index of the menu item in the receiver that has submenu anObject.
public int indexOfItemWithSubmenu
(NSMenu anObject)
If no such menu item is in the menu, returns –1.
Returns the index of the first menu item in the receiver identified by tag aTag.
public int indexOfItemWithTag
(int aTag)
If no such menu item is in the menu, returns –1.
Returns the index of the first menu item in the receiver that has target anObject and action actionSelector.
public int indexOfItemWithTargetAndAction
(Object anObject, NSSelector actionSelector)
If actionSelector is NULL
, the first menu item in the receiver that has target anObject is returned. If no menu item matching these criteria is in the menu, returns –1.
Returns the index of the first menu item in the receiver that has the title aTitle.
public int indexOfItemWithTitle
(String aTitle)
If no such menu item is in the menu, returns –1.
Inserts the menu item newItem in the receiver at location index.
public void insertItemAtIndex
(NSMenuItem newItem, int index)
If the menu item already exists in another menu, it is not inserted. This method posts an MenuDidAddItemNotification
, allowing interested observers to update as appropriate. It also causes the menu to update itself. This method is a primitive method. All item addition methods end up calling this method, so this is where you should implement custom behavior on adding new items to a menu in a custom subclass.
Adds a new item at location index in the receiver with title aString, action aSelector, and key equivalent keyEquiv.
public NSMenuItem insertItemAtIndex
(String aString, NSSelector aSelector, String keyEquiv, int index)
Returns the new menu item. If you do not want the menu item to have a key equivalent, keyEquiv should be an empty string and not null
.
Returns true
if the receiver is currently attached to another menu, false
otherwise.
public boolean isAttached
()
Returns false
if the receiver is offscreen or attached to another menu (or if it’s the main menu), true
otherwise.
public boolean isTornOff
()
Returns the receiver’s menu items.
public NSArray itemArray
()
Returns the menu item at location index of the receiver.
public NSMenuItem itemAtIndex
(int index)
It throws an exception if index is out of bounds.
Invoked when menu item anObject is modified visually (for example, its title changes).
public void itemChanged
(NSMenuItem anObject)
It is not called for action, target, represented object, or tag changes. Posts a MenuDidChangeItemNotification
.
Returns the first menu item in the receiver with tag aTag.
public NSMenuItem itemWithTag
(int aTag)
Returns the first menu item in the receiver with title aString.
public NSMenuItem itemWithTitle
(String aString)
public NSPoint locationForSubmenu
(NSMenu aSubmenu)
Returns the screen coordinates where aSubmenu will be displayed when opened as a submenu of the receiver.
Returns the menu bar height for the current application’s main menu.
public float menuBarHeight()
Returns 0.0 if the receiver is some other menu. This method supersedes the "menuBarHeight"
class method of the NSMenuView class.
Returns true
if messages are being sent to the application’s windows upon each change to the receiver, false
otherwise.
public boolean menuChangedMessagesEnabled
()
Deprecated. Returns null
.
public Object menuRepresentation
()
Returns the number of menu items in the receiver, including separator items.
public int numberOfItems
()
Causes the application to send the action message of the menu item at index to its target.
public void performActionForItemAtIndex
(int index)
If a target is not specified, the message is sent to the first responder. As a side effect, this method posts MenuWillSendActionNotification
and MenuDidSendActionNotification
.
public boolean performKeyEquivalent
(NSEvent theEvent)
Performs the action for the menu item that corresponds to the key equivalent in theEvent.
Removes anItem from the receiver.
public void removeItem
(NSMenuItem anItem)
Removes the menu item at location index.
public void removeItemAtIndex
(int index)
Posts a MenuDidRemoveItemNotification
.
Controls whether the receiver automatically enables and disables its menu items based on delegates implementing the NSMenu.MenuValidation interface.
public void setAutoenablesItems
(boolean flag)
If flag is true
, menu items are automatically enabled and disabled. If flag is false
, menu items are not automatically enabled or disabled. See the “NSMenu.MenuValidation” interface specification for more information.
Deprecated. Mac OS X does not use menu representations to draw menus.
public void setContextMenuRepresentation
(Object menuRep)
Sets the receiver’s delegate.
public void setDelegate
(Object anObject)
You can use the delegate to populate a menu just before it is going to be drawn and to check for key equivalents without creating a menu item.
Controls whether the receiver sends messages to the application’s windows upon each menu change.
public void setMenuChangedMessagesEnabled
(boolean flag)
To avoid the “flickering” effect of many successive menu changes, invoke this method with flag set to false
, make changes to the menu, and invoke the method again with flag set to true
. This approach has the effect of batching changes and applying them all at once.
Deprecated. Mac OS X does not use menu representations to draw menus.
public void setMenuRepresentation
(Object menuRep)
Makes aMenu a submenu controlled by anItem, automatically setting the action of anItem to submenuAction
.
public void setSubmenuForItem
(NSMenu aMenu, NSMenuItem anItem)
Sets the receiver’s supermenu to supermenu.
public void setSupermenu
(NSMenu supermenu)
You should never invoke this method directly; it is public so subclassers can add behavior to the default implementation. Subclassers should call the superclass’s method as part of their implementation.
Deprecated. Mac OS X does not use menu representations to draw menus.
public void setTearOffMenuRepresentation
(Object menuRep)
Sets the receiver’s title to aString.
public void setTitle
(String aString)
Resizes the receiver to exactly fit its items.
public void sizeToFit
()
This method is the action method assigned to menu items that open submenus.
public void submenuAction
(Object sender)
Never invoke this method directly.
Returns the receiver’s supermenu or null
if it has none.
public NSMenu supermenu
()
Deprecated. Returns null
public Object tearOffMenuRepresentation
()
.
Returns the receiver’s title.
public String title
()
Enables or disables the receiver’s menu items based on the NSMenu.MenuValidation interface and sizes the menu to fit its current menu items if necessary.
public void update
()
See the “NSMenu.MenuValidation” interface specification for more information.
Called to allow the delegate to return the key down event has a key equivalent.
public abstract boolean menuHasKeyEquivalent
(NSMenu menu, NSEvent event)
If there is a valid and enabled menu item that corresponds to this key down even, return true
. Return false
if there are no items with that key equivalent or if the item is disabled. If the delegate does not define this method, the menu is populated to find out if any items have a matching key equivalent.
Called to allow the delegate to return the action for a key down event.
public abstract NSSelector menuKeyEquivalentAction
(NSMenu menu, NSEvent event)
If there is a valid and enabled menu item that corresponds to this key down even, return the action. Return null
if there are no items with that key equivalent or if the item is disabled.
Called to allow the delegate to return the target for a key down event.
public abstract Object menuKeyEquivalentTarget
(NSMenu menu, NSEvent event)
If there is a valid and enabled menu item that corresponds to this key down even, return the target. Return null
if there are no items with that key equivalent or if the item is disabled.
Called when a menu is about to be displayed at the start of a tracking session so the delegate can modify the menu.
public abstract void menuNeedsUpdate
(NSMenu menu)
You can change the menu by adding, removing or modifying menu items. Be sure to set the proper enable state for any new menu items. If populating the menu will take a long time, implement numberOfItemsInMenu
and menuUpdateItemAtIndex
instead.
Called to let you update a menu item before it is displayed.
public abstract boolean menuUpdateItemAtIndex
(NSMenu menu, NSMenuItem item, int index, boolean shouldCancel)
If your numberOfItemsInMenu
delegate method returns a positive value, then your menuUpdateItemAtIndex
method is called for each item in the menu. You can then update the menu title, image, and so forth for the menu item. Return true
to continue the process. If you return false
, your menuUpdateItemAtIndex
is not called again. In that case, it is your responsibility to trim any extra items from the menu.
The shouldCancel parameter is set to true
when your delegate is called if, due to some user action, the menu no longer needs to be displayed before all the menu items have been updated. You can ignore this flag, return true
, and continue; or you can save your work (to save time the next time your delegate is called) and return false
to stop the updating.
Called when a menu is about to be displayed at the start of a tracking session so the delegate can specify the number of items in the menu.
public abstract int numberOfItemsInMenu
(NSMenu menu)
If you return a positive value, the menu is resized by either removing or adding items. Newly created items are blank. After the menu is resized, your menuUpdateItemAtIndex
method is called for each item. If you return a negative value, the number of items is left unchanged and menuUpdateItemAtIndex
is not called. If you can populate the menu quickly, you can implement menuNeedsUpdate
instead of numberOfItemsInMenu
and menuUpdateItemAtIndex
.
Posted after a menu item is added to the menu. The notification object is the NSMenu that just added the new menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
The integer index of the menu item that was added. |
Posted after a menu item in the menu changes appearance. Changes include enabling/disabling, changes in state, and changes to title. The notification object is the NSMenu with the menu item that changed. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
The integer index of the menu item that changed. |
Posted when menu tracking ends, even if no action is sent. The notification object is the main menu bar (NSApplication.sharedApplication().mainMenu.()
) or the root menu of a popup button. This notification does not contain a userInfo dictionary.
Posted after a menu item is removed from the menu. The notification object is the NSMenu that just removed the menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
The integer index of the menu item that was removed. Note that this index may no longer be valid and in any event no longer points to the menu item that was removed. |
Posted just after the application dispatches a menu item’s action method to the menu item’s target. The notification object is the NSMenu containing the chosen menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
The menu item that was chosen. |
Posted just before the application dispatches a menu item’s action method to the menu item’s target. The notification object is the NSMenu containing the chosen menu item. The userInfo dictionary contains the following information:
Key |
Value |
---|---|
|
The menu item that was chosen. |
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-02-01)