Class WPopupMenu


public class WPopupMenu
extends WMenu
A menu presented in a popup window.

The menu implements a typical context menu, with support for submenu's. It is a specialized WMenu from which it inherits most of the API.

When initially created, the menu is invisible, until popup() or exec() is called. Then, the menu will remain visible until an item is selected, or the user cancels the menu (by hitting Escape or clicking elsewhere).

The implementation assumes availability of JavaScript to position the menu at the current mouse position and provide feed-back of the currently selected item.

As with WDialog, there are two ways of using the menu. The simplest way is to use one of the synchronous exec() methods, which starts a reentrant event loop and waits until the user cancelled the popup menu (by hitting Escape or clicking elsewhere), or selected an item.

Alternatively, you can use one of the popup() methods to show the menu and listen to the triggered() signal where you read the getResult(), or associate the menu with a button using WPushButton#setMenu().

You have several options to react to the selection of an item:

Usage example:


 // Create a menu with some items
 WPopupMenu popup = new WPopupMenu();
 popup.addItem("icons/item1.gif", "Item 1");
 popup.addItem("Item 2").setCheckable(true);
 popup.addItem("Item 3");
 popup.addSeparator();
 popup.addItem("Item 4");
 popup.addSeparator();
 popup.addItem("Item 5");
 popup.addItem("Item 6");
 popup.addSeparator();

 WPopupMenu subMenu = new WPopupMenu();
 subMenu.addItem("Sub Item 1");
 subMenu.addItem("Sub Item 2");
 popup.addMenu("Item 7", subMenu);

 WMenuItem item = popup.exec(event);

 if (item != null) {
 // ... do associated action.
 }

 

A snapshot of the WPopupMenu:

WPopupMenu example (default)

WPopupMenu example (polished)

See Also:
WMenuItem
  • Constructor Details

  • Method Details

    • remove

      public void remove()
      Description copied from class: WMenu
      Destructor.
      Overrides:
      remove in class WMenu
      See Also:
      WWidget.removeWidget(WWidget widget)
    • popup

      public void popup​(WPoint p)
      Shows the the popup at a position.

      Displays the popup at a point with document coordinates point. The positions intelligent, and will chose one of the four menu corners to correspond to this point so that the popup menu is completely visible within the window.

    • popup

      public void popup​(WMouseEvent e)
      Shows the the popup at the location of a mouse event.

      This is a convenience method for popup() that uses the event's document coordinates.

      See Also:
      popup(WPoint p), WMouseEvent.getDocument()
    • setButton

      public void setButton​(WInteractWidget button)
    • popup

      public void popup​(WWidget location, Orientation orientation)
      Shows the popup besides a widget.

      See Also:
      WWidget.positionAt(WWidget widget, Orientation orientation)
    • popup

      public final void popup​(WWidget location)
      Shows the popup besides a widget.

      Calls popup(location, Orientation.Vertical)

    • exec

      public WMenuItem exec​(WPoint p)
      Executes the the popup at a position.

      Displays the popup at a point with document coordinates p, using popup(), and the waits until a menu item is selected, or the menu is cancelled.

      Returns the selected menu (or sub-menu) item, or null if the user cancelled the menu.

      See Also:
      popup(WPoint p)
    • exec

      public WMenuItem exec​(WMouseEvent e)
      Executes the the popup at the location of a mouse event.

      This is a convenience method for exec() that uses the event's document coordinates.

      See Also:
      exec(WPoint p)
    • exec

      public WMenuItem exec​(WWidget location, Orientation orientation)
      Executes the popup besides a widget.

      See Also:
      WWidget.positionAt(WWidget widget, Orientation orientation)
    • exec

      public final WMenuItem exec​(WWidget location)
      Executes the popup besides a widget.

      Returns exec(location, Orientation.Vertical)

    • getResult

      public WMenuItem getResult()
      Returns the last triggered menu item.

      The result is null when the user cancelled the popup menu.

    • setHidden

      public void setHidden​(boolean hidden, WAnimation animation)
      Description copied from class: WWidget
      Hides or shows the widget.

      Hides or show the widget (including all its descendant widgets). When setting hidden = false, this widget and all descendant widgets that are not hidden will be shown. A widget is only visible if it and all its ancestors in the widget tree are visible, which may be checked using isVisible().

      Overrides:
      setHidden in class WCompositeWidget
    • setMaximumSize

      public void setMaximumSize​(WLength width, WLength height)
      Description copied from class: WWidget
      Sets a maximum size.

      Specifies a maximum size for this widget, setting CSS max-width and max-height properties.

      The default the maximum width and height are WLength.Auto, indicating no maximum size. A LengthUnit.Percentage size should not be used, as this is (in virtually all cases) undefined behaviour.

      When the widget is a container widget that contains a layout manager, then setting a maximum size will have the effect of letting the size of the container to reflect the preferred size of the contents (rather than constraining the size of the children based on the size of the container), up to the specified maximum size.

      Overrides:
      setMaximumSize in class WCompositeWidget
      See Also:
      WWidget.resize(WLength width, WLength height), WWidget.setMinimumSize(WLength width, WLength height)
    • setMinimumSize

      public void setMinimumSize​(WLength width, WLength height)
      Description copied from class: WWidget
      Sets a minimum size.

      Specifies a minimum size for this widget, setting CSS min-width and min-height properties.

      The default minimum width and height is 0. The special value WLength.Auto indicates that the initial width is used as minimum size. A LengthUnit.Percentage size should not be used, as this is (in virtually all cases) undefined behaviour.

      When the widget is inserted in a layout manager, then the minimum size will be taken into account.

      Overrides:
      setMinimumSize in class WCompositeWidget
      See Also:
      WWidget.resize(WLength width, WLength height), WWidget.getMinimumWidth(), WWidget.getMinimumHeight()
    • aboutToHide

      public Signal aboutToHide()
      Signal emitted when the popup is hidden.

      Unlike the WMenu.itemSelected() signal, aboutToHide() is only emitted by the toplevel popup menu (and not by submenus), and is also emitted when no item was selected.

      You can use getResult() to get the selected item, which may be null.

      See Also:
      triggered(), WMenu.itemSelected()
    • triggered

      public Signal1<WMenuItem> triggered()
      Signal emitted when an item is selected.

      Unlike the WMenu.itemSelected() signal, triggered() is only emitted by the toplevel popup menu (and not by submenus).

      See Also:
      aboutToHide(), WMenu.itemSelected()
    • setAutoHide

      public void setAutoHide​(boolean enabled, int autoHideDelay)
      Configure auto-hide when the mouse leaves the menu.

      If enabled, The popup menu will be hidden when the mouse leaves the menu for longer than autoHideDelay (milliseconds). The popup menu result will be 0, as if the user cancelled.

      By default, this option is disabled.

    • setAutoHide

      public final void setAutoHide​(boolean enabled)
      Configure auto-hide when the mouse leaves the menu.

      Calls setAutoHide(enabled, 0)

    • setHideOnSelect

      public void setHideOnSelect​(boolean enabled)
      Set whether this popup menu should hide when an item is selected.

      Defaults to true.

      See Also:
      isHideOnSelect()
    • setHideOnSelect

      public final void setHideOnSelect()
      Set whether this popup menu should hide when an item is selected.

      Calls setHideOnSelect(true)

    • isHideOnSelect

      public boolean isHideOnSelect()
      Returns whether this popup menu should hide when an item is selected.

      See Also:
      setHideOnSelect(boolean enabled)
    • renderSelected

      protected void renderSelected​(WMenuItem item, boolean selected)
      Overrides:
      renderSelected in class WMenu
    • setCurrent

      protected void setCurrent​(int index)
      Overrides:
      setCurrent in class WMenu
    • render

      protected void render​(java.util.EnumSet<RenderFlag> flags)
      Description copied from class: WWidget
      Renders the widget.

      This function renders the widget (or an update for the widget), after this has been scheduled using scheduleRender().

      The default implementation will render the widget by serializing changes to JavaScript and HTML. You may want to reimplement this widget if you have been postponing some of the layout / rendering implementation until the latest moment possible. In that case you should make sure you call the base implementation however.

      Overrides:
      render in class WMenu