Class Menu

java.lang.Object
org.eclipse.swt.widgets.Widget
org.eclipse.swt.widgets.Menu

public class Menu extends Widget
Instances of this class are user interface objects that contain menu items.
Styles:
BAR, DROP_DOWN, POP_UP, NO_RADIO_GROUP
LEFT_TO_RIGHT, RIGHT_TO_LEFT
Events:
Help, Hide, Show

Note: Only one of BAR, DROP_DOWN and POP_UP may be specified. Only one of LEFT_TO_RIGHT or RIGHT_TO_LEFT may be specified.

IMPORTANT: This class is not intended to be subclassed.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    int
    the handle to the OS resource (Warning: This field is platform dependent)
  • Constructor Summary

    Constructors
    Constructor
    Description
    Menu(Control parent)
    Constructs a new instance of this class given its parent, and sets the style for the instance so that the instance will be a popup menu on the given parent's shell.
    Menu(Decorations parent, int style)
    Constructs a new instance of this class given its parent (which must be a Decorations) and a style value describing its behavior and appearance.
    Menu(Menu parentMenu)
    Constructs a new instance of this class given its parent (which must be a Menu) and sets the style for the instance so that the instance will be a drop-down menu on the given parent's parent.
    Menu(MenuItem parentItem)
    Constructs a new instance of this class given its parent (which must be a MenuItem) and sets the style for the instance so that the instance will be a drop-down menu on the given parent's parent menu.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds the listener to the collection of listeners who will be notified when help events are generated for the control, by sending it one of the messages defined in the HelpListener interface.
    void
    Adds the listener to the collection of listeners who will be notified when menus are hidden or shown, by sending it one of the messages defined in the MenuListener interface.
    Returns the default menu item or null if none has been previously set.
    boolean
    Returns true if the receiver is enabled, and false otherwise.
    getItem(int index)
    Returns the item at the given, zero-relative index in the receiver.
    int
    Returns the number of items contained in the receiver.
    Returns a (possibly empty) array of MenuItems which are the items in the receiver.
    int
    Returns the orientation of the receiver, which will be one of the constants SWT.LEFT_TO_RIGHT or SWT.RIGHT_TO_LEFT.
    Returns the receiver's parent, which must be a Decorations.
    Returns the receiver's parent item, which must be a MenuItem or null when the receiver is a root.
    Returns the receiver's parent item, which must be a Menu or null when the receiver is a root.
    Returns the receiver's shell.
    boolean
    Returns true if the receiver is visible, and false otherwise.
    int
    Searches the receiver's list starting at the first item (index 0) until an item is found that is equal to the argument, and returns the index of that item.
    boolean
    Returns true if the receiver is enabled and all of the receiver's ancestors are enabled, and false otherwise.
    boolean
    Returns true if the receiver is visible and all of the receiver's ancestors are visible and false otherwise.
    void
    Removes the listener from the collection of listeners who will be notified when the help events are generated for the control.
    void
    Removes the listener from the collection of listeners who will be notified when the menu events are generated for the control.
    void
    Sets the default menu item to the argument or removes the default emphasis when the argument is null.
    void
    setEnabled(boolean enabled)
    Enables the receiver if the argument is true, and disables it otherwise.
    void
    setLocation(int x, int y)
    Sets the location of the receiver, which must be a popup, to the point specified by the arguments which are relative to the display.
    void
    setLocation(Point location)
    Sets the location of the receiver, which must be a popup, to the point specified by the argument which is relative to the display.
    void
    setOrientation(int orientation)
    Sets the orientation of the receiver, which must be one of the constants SWT.LEFT_TO_RIGHT or SWT.RIGHT_TO_LEFT.
    void
    setVisible(boolean visible)
    Marks the receiver as visible if the argument is true, and marks it invisible otherwise.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • handle

      public int handle
      the handle to the OS resource (Warning: This field is platform dependent)

      IMPORTANT: This field is not part of the SWT public API. It is marked public only so that it can be shared within the packages provided by SWT. It is not available on all platforms and should never be accessed from application code.

  • Constructor Details

    • Menu

      public Menu(Control parent)
      Constructs a new instance of this class given its parent, and sets the style for the instance so that the instance will be a popup menu on the given parent's shell.

      After constructing a menu, it can be set into its parent using parent.setMenu(menu). In this case, the parent may be any control in the same widget tree as the parent.

      Parameters:
      parent - a control which will be the parent of the new instance (cannot be null)
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the parent is null
      SWTException -
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
      • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
      See Also:
    • Menu

      public Menu(Decorations parent, int style)
      Constructs a new instance of this class given its parent (which must be a Decorations) and a style value describing its behavior and appearance.

      The style value is either one of the style constants defined in class SWT which is applicable to instances of this class, or must be built by bitwise OR'ing together (that is, using the int "|" operator) two or more of those SWT style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.

      After constructing a menu or menuBar, it can be set into its parent using parent.setMenu(menu) or parent.setMenuBar(menuBar).

      Parameters:
      parent - a decorations control which will be the parent of the new instance (cannot be null)
      style - the style of menu to construct
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the parent is null
      SWTException -
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
      • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
      See Also:
    • Menu

      public Menu(Menu parentMenu)
      Constructs a new instance of this class given its parent (which must be a Menu) and sets the style for the instance so that the instance will be a drop-down menu on the given parent's parent.

      After constructing a drop-down menu, it can be set into its parentMenu using parentMenu.setMenu(menu).

      Parameters:
      parentMenu - a menu which will be the parent of the new instance (cannot be null)
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the parent is null
      SWTException -
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
      • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
      See Also:
    • Menu

      public Menu(MenuItem parentItem)
      Constructs a new instance of this class given its parent (which must be a MenuItem) and sets the style for the instance so that the instance will be a drop-down menu on the given parent's parent menu.

      After constructing a drop-down menu, it can be set into its parentItem using parentItem.setMenu(menu).

      Parameters:
      parentItem - a menu item which will be the parent of the new instance (cannot be null)
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the parent is null
      SWTException -
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
      • ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass
      See Also:
  • Method Details

    • addHelpListener

      public void addHelpListener(HelpListener listener)
      Adds the listener to the collection of listeners who will be notified when help events are generated for the control, by sending it one of the messages defined in the HelpListener interface.
      Parameters:
      listener - the listener which should be notified
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the listener is null
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      See Also:
    • addMenuListener

      public void addMenuListener(MenuListener listener)
      Adds the listener to the collection of listeners who will be notified when menus are hidden or shown, by sending it one of the messages defined in the MenuListener interface.
      Parameters:
      listener - the listener which should be notified
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the listener is null
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
      See Also:
    • getDefaultItem

      public MenuItem getDefaultItem()
      Returns the default menu item or null if none has been previously set.
      Returns:
      the default menu item.
    Throws:
    SWTException -
    • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
    • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver