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 Details

    • x

      int x
    • y

      int y
    • hasLocation

      boolean hasLocation
    • cascade

      MenuItem cascade
    • selectedItem

      MenuItem selectedItem
    • parent

      Decorations parent
    • imageList

      ImageList imageList
    • poppedUpCount

      int poppedUpCount
    • modelHandle

      long modelHandle
      GTK4 only fields
    • actionGroup

      long actionGroup
      GTK4 only fields
    • shortcutController

      long shortcutController
      GTK4 only fields
    • sections

    • items

  • 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

    • checkNull

      static Control checkNull(Control control)
    • checkNull

      static Menu checkNull(Menu menu)
    • checkNull

      static MenuItem checkNull(MenuItem item)
    • checkStyle

      static int checkStyle(int style)
    • ableToSetLocation

      boolean ableToSetLocation()
      Bug 532074: setLocation method is limited on Wayland since Wayland has no global coordinates and we cannot use gdk_popup_at_rect if GdkWindow of getShell is not mapped. In this case, we can only pop the menu at the pointer. This happens for example when Problems view is a fast view on Eclipse start, the drop down menu takes PartRenderingEngine's limbo shell (see PartRenderingEngine#safeCreateGui) which is off the screen.
      Returns:
      true iff the location of menu is set and can be used successfully
    • _setVisible

      void _setVisible(boolean visible)
    • addAccelerators

      void addAccelerators(long accelGroup)
    • 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:
    • 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:
    • createHandle

      void createHandle(int index)
      Overrides:
      createHandle in class Widget
    • createWidget

      void createWidget(int index)
      Overrides:
      createWidget in class Widget
    • fixMenus

      void fixMenus(Decorations newParent)
    • 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
    • getEnabled

      public boolean getEnabled()
      Returns true if the receiver is enabled, and false otherwise. A disabled menu is typically not selectable from the user interface and draws with an inactive or "grayed" look.
      Returns:
      the receiver's enabled state
      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
      See Also:
    • getItem

      public MenuItem getItem(int index)
      Returns the item at the given, zero-relative index in the receiver. Throws an exception if the index is out of range.
      Parameters:
      index - the index of the item to return
      Returns:
      the item at the given index
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
    • getItemCount

      public int getItemCount()
      Returns the number of items contained in the receiver.
      Returns:
      the number of items
      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
    • getItems

      public MenuItem[] getItems()
      Returns a (possibly empty) array of MenuItems which are the items in the receiver.

      Note: This is not the actual structure used by the receiver to maintain its list of items, so modifying the array will not affect the receiver.

      Returns:
      the items in the receiver
      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
    • getNameText

      String getNameText()
      Overrides:
      getNameText in class Widget
    • getOrientation

      public int getOrientation()
      Returns the orientation of the receiver, which will be one of the constants SWT.LEFT_TO_RIGHT or SWT.RIGHT_TO_LEFT.
      Returns:
      the orientation style
      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
      Since:
      3.7
    • getParent

      public Decorations getParent()
      Returns the receiver's parent, which must be a Decorations.
      Returns:
      the receiver's parent
      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
    • getParentItem

      public MenuItem getParentItem()
      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
      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
    • getParentMenu

      public Menu getParentMenu()
      Returns the receiver's parent item, which must be a Menu or null when the receiver is a root.
      Returns:
      the receiver's parent 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
    • getShell

      public Shell getShell()
      Returns the receiver's shell. For all controls other than shells, this simply returns the control's nearest ancestor shell. Shells return themselves, even if they are children of other shells. Returns null if receiver or its ancestor is the application menubar.
      Returns:
      the receiver's shell or null
      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
      See Also:
    • getVisible

      public boolean getVisible()
      Returns true if the receiver is visible, and false otherwise.

      If one of the receiver's ancestors is not visible or some other condition makes the receiver not visible, this method may still indicate that it is considered visible even though it may not actually be showing.

      Returns:
      the receiver's visibility state
      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
    • gtk_hide

      long gtk_hide(long widget)
      Overrides:
      gtk_hide in class Widget
    • gtk_show

      long gtk_show(long widget)
      Overrides:
      gtk_show in class Widget
    • gtk3_show_help

      long gtk3_show_help(long widget, long helpType)
      Overrides:
      gtk3_show_help in class Widget
    • gtk_menu_popped_up

      long gtk_menu_popped_up(long widget, long flipped_rect, long final_rect, long flipped_x, long flipped_y)
      Description copied from class: Widget

      GTK3.22+ has API which allows clients of GTK to connect a menu to the "popped-up" signal. This callback is triggered after the menu is popped up/shown to the user, and provides information about the actual position and size of the menu, as shown to the user.

      SWT clients can enable this functionality by launching their application with the SWT_MENU_LOCATION_DEBUGGING environment variable set to 1. If enabled, the previously mentioned positioning and size information will be printed to the console. The information comes from GTK internals and is stored in the method parameters.

      Overrides:
      gtk_menu_popped_up in class Widget
      Parameters:
      widget - the memory address of the menu which was popped up
      flipped_rect - a pointer to the GdkRectangle containing the flipped location and size of the menu
      final_rect - a pointer to the GdkRectangle containing the final (after all internal adjustments) location and size of the menu
      flipped_x - a boolean flag indicating whether the menu has been inverted along the X-axis
      flipped_y - a boolean flag indicating whether the menu has been inverted along the Y-axis
    • hookEvents

      void hookEvents()
      Overrides:
      hookEvents in class Widget
    • indexOf

      public int indexOf(MenuItem item)
      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. If no item is found, returns -1.
      Parameters:
      item - the search item
      Returns:
      the index of the item
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the item 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
    • isEnabled

      public boolean isEnabled()
      Returns true if the receiver is enabled and all of the receiver's ancestors are enabled, and false otherwise. A disabled menu is typically not selectable from the user interface and draws with an inactive or "grayed" look.
      Returns:
      the receiver's enabled state
      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
      See Also:
    • isVisible

      public boolean isVisible()
      Returns true if the receiver is visible and all of the receiver's ancestors are visible and false otherwise.
      Returns:
      the receiver's visibility state
      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
      See Also:
    • releaseChildren

      void releaseChildren(boolean destroy)
      Overrides:
      releaseChildren in class Widget
    • releaseParent

      void releaseParent()
      Overrides:
      releaseParent in class Widget
    • releaseWidget

      void releaseWidget()
      Overrides:
      releaseWidget in class Widget
    • destroyWidget

      void destroyWidget()
      Overridden to fix memory leak on GTK3, see bug 573983
      Overrides:
      destroyWidget in class Widget
    • removeMenuListener

      public void removeMenuListener(MenuListener listener)
      Removes the listener from the collection of listeners who will be notified when the menu events are generated for the control.
      Parameters:
      listener - the listener which should no longer 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:
    • removeAccelerators

      void removeAccelerators(long accelGroup)
    • removeHelpListener

      public void removeHelpListener(HelpListener listener)
      Removes the listener from the collection of listeners who will be notified when the help events are generated for the control.
      Parameters:
      listener - the listener which should no longer 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:
    • reskinChildren

      void reskinChildren(int flags)
      Overrides:
      reskinChildren in class Widget
    • sendHelpEvent

      boolean sendHelpEvent(long helpType)
    • setDefaultItem

      public void setDefaultItem(MenuItem item)
      Sets the default menu item to the argument or removes the default emphasis when the argument is null.
      Parameters:
      item - the default menu item or null
      Throws:
      IllegalArgumentException -
      • ERROR_INVALID_ARGUMENT - if the menu item has been disposed
      SWTException -
      • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
      • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
    • setEnabled

      public void setEnabled(boolean enabled)
      Enables the receiver if the argument is true, and disables it otherwise. A disabled menu is typically not selectable from the user interface and draws with an inactive or "grayed" look.
      Parameters:
      enabled - the new enabled state
      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
    • setLocation

      public 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.

      Note that this is different from most widgets where the location of the widget is relative to the parent.

      Also note that the actual location of the menu is dependent on platform specific behavior. For example: on Linux with Wayland this operation is a hint due to lack of global coordinates.

      Parameters:
      x - the new x coordinate for the receiver
      y - the new y coordinate for the receiver
      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
    • setLocation

      public 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.

      Note that this is different from most widgets where the location of the widget is relative to the parent.

      Note that the platform window manager ultimately has control over the location of popup menus.

      Parameters:
      location - the new location for the receiver
      Throws:
      IllegalArgumentException -
      • ERROR_NULL_ARGUMENT - if the point 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
      Since:
      2.1
    • setOrientation

      public 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.
      Parameters:
      orientation - new orientation style
      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
      Since:
      3.7
    • _setOrientation

      void _setOrientation(int orientation)
    • setOrientation

      void setOrientation(boolean create)
      Overrides:
      setOrientation in class Widget
    • adjustParentWindowWayland

      void adjustParentWindowWayland(long eventPtr)
      Lack of absolute coordinates make Wayland event windows inaccurate. Currently the best approach is to the use the GdkWindow of the mouse pointer. See bug 530059 and 532074.

      Parameters:
      eventPtr - a pointer to the GdkEvent
    • verifyMenuPosition

      void verifyMenuPosition(int itemCount)
      Feature in GTK3 on X11: context menus in SWT are populated dynamically, sometimes asynchronously outside of SWT (i.e. in Platform UI). This means that items are added and removed just before the menu is shown. This method of changing the menu content can sometimes cause sizing issues internally in GTK, specifically with the height of the toplevel GdkWindow.

      The fix is to cache the number of items popped up previously, and if the number of items in the current menu (to be popped up) is different, then:

      • get the preferred height of the menu
      • set the toplevel GdkWindow to that height
      Parameters:
      itemCount - the current number of items in the menu, just before it's about to be shown/popped-up
    • setVisible

      public void setVisible(boolean visible)
      Marks the receiver as visible if the argument is true, and marks it invisible otherwise.

      If one of the receiver's ancestors is not visible or some other condition makes the receiver not visible, marking it visible may not actually cause it to be displayed.

      Parameters:
      visible - the new visibility state
      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