Class MenuItem

All Implemented Interfaces:
AttachNotifier, ClickNotifier<MenuItem>, DetachNotifier, HasAriaLabel, HasComponents, HasComponentsOfType<Component>, HasElement, HasEnabled, HasStyle, HasText, Serializable

public class MenuItem extends MenuItemBase<ContextMenu,MenuItem,SubMenu> implements ClickNotifier<MenuItem>
Item component used inside ContextMenu and SubMenu. This component can be created and added to a menu overlay with HasMenuItems.addItem(String, ComponentEventListener) and similar methods.
Since:
1.0
Author:
Vaadin Ltd.
See Also:
  • Constructor Details

  • Method Details

    • createSubMenu

      protected SubMenu createSubMenu()
      Specified by:
      createSubMenu in class MenuItemBase<ContextMenu,MenuItem,SubMenu>
    • setEnabled

      public void setEnabled(boolean enabled)
      Sets the menu item explicitly disabled or enabled. When disabled, menu items are rendered as "dimmed".

      By default, disabled items are not focusable and don't react to hover. As a result, they are hidden from assistive technologies, and it's not possible to show a tooltip to explain why they are disabled. This can be addressed by enabling the feature flag accessibleDisabledMenuItems, which makes disabled items focusable and hoverable, while still preventing them from being activated. To enable this feature flag, add the following line to src/main/resources/vaadin-featureflags.properties:

       com.vaadin.experimental.accessibleDisabledMenuItems = true
       
      Specified by:
      setEnabled in interface HasEnabled
      Overrides:
      setEnabled in class MenuItemBase<ContextMenu,MenuItem,SubMenu>
      Parameters:
      enabled - if false then explicitly disables the object, if true then enables the object so that its state depends on parent
      Since:
      25.2
    • getDownloadHandler

      public DownloadHandler getDownloadHandler()
      Gets the handler that produces the file downloaded when the item is clicked.
      Returns:
      the download handler, or null if clicking the item does not start a download
      Since:
      25.4
      See Also:
    • setDownloadHandler

      public void setDownloadHandler(DownloadHandler downloadHandler)
      Sets a handler that produces a file to download when the item is clicked. The download starts in the browser as part of the click, and click listeners still run as usual. Setting a new handler replaces the previous one, and null stops the item from starting a download.

      The handler runs only when the browser requests the file, once per click, so you can create the content, file name and response headers at that point. If the handler fails, the browser reports a failed download instead of saving an empty file. Use the transfer callbacks of the handler, for example whenComplete, to react on the server when the transfer has finished or failed.

      The file is only served while the item is in its menu, visible and enabled, so hiding or removing the item in a click listener makes the download fail. The exception is an item with disable on click: it disables itself before the browser requests the file, so the file is also served while the item is disabled by the click. Once the enabled state is set explicitly, for example with setEnabled(false), or the menu is disabled, the file is no longer served while the item is disabled. To serve the file regardless of the enabled state, pass handler.allowDisabled().

      For an item of a ContextMenu, the file is served on behalf of the target of the menu, so the download only works while the menu has a target that is attached. Closing the menu does not affect the download. The URL of the file stays valid while the target is attached, also when the menu is closed, so hide or disable the item to stop serving the file. The owning component of the download is the target of the menu.

      For an item of a MenuBar, the file is served on behalf of the item itself, so the download only works while the menu bar is attached. The owning component of the download is the item.

      
       MenuItem export = menu.addItem("Export");
       export.setDownloadHandler(
               DownloadHandler.fromInputStream(event -> createReport()));
       
      Parameters:
      downloadHandler - the handler that produces the file, or null to not start a download on click
      Since:
      25.4
    • getMenuDownloadManager

      protected MenuDownloadManager getMenuDownloadManager()
      Gets the manager for the download registrations of the menu that this item belongs to.

      For internal use only. May be renamed or removed in a future release.

      Returns:
      the manager for the download registrations, or null if the item does not belong to a menu