Class MenuItem

java.lang.Object
limn.components.MenuItem

public final class MenuItem extends Object
One entry in a Menu: a command, a checkable toggle, a submenu, or a separator. Pure model (no widgets): a PopupMenu or MenuBar renders it. Build with the static factories:

 Menu edit = new Menu()
     .add(MenuItem.of("Undo", () -> undo()).setAccelerator(Accelerator.command(Keys.Z)))
     .addSeparator()
     .add(MenuItem.check("Word wrap", true, on -> setWrap(on)).setMnemonic('W'))
     .add(MenuItem.submenu("Export", exportMenu));
 
  • Method Details

    • of

      public static MenuItem of(String label, Runnable action)
      A command that runs action when chosen.
    • of

      public static MenuItem of(I18nString label, Runnable action)
      A command whose label follows the UI language; see I18nString.
    • check

      public static MenuItem check(String label, boolean checked, Consumer<Boolean> onToggle)
      A checkable item showing a check mark for its state; choosing it flips the state and calls onToggle with the new value.
    • check

      public static MenuItem check(I18nString label, boolean checked, Consumer<Boolean> onToggle)
      A checkable item whose label follows the UI language.
    • separator

      public static MenuItem separator()
      A non-interactive divider line between groups of items.
    • label

      public String label()
      The label as it currently reads, or null for a separator.
    • labelSource

      public I18nString labelSource()
      The localizable value behind label(); null for a separator.
    • isEnabled

      public boolean isEnabled()
      Whether the item can be chosen; a disabled one still occupies a row.
    • setEnabled

      public MenuItem setEnabled(boolean value)
      Enables or disables the item.
    • isChecked

      public boolean isChecked()
      Returns:
      whether a check(java.lang.String, boolean, java.util.function.Consumer<java.lang.Boolean>) item is currently checked
    • setChecked

      public MenuItem setChecked(boolean value)
      Sets a check item's state (does not fire onToggle). UI thread when displayed.
    • accelerator

      public Accelerator accelerator()
      The keyboard shortcut shown at the right of the row and dispatched while closed, or null.
    • setAccelerator

      public MenuItem setAccelerator(Accelerator value)
      Declares the keyboard shortcut that runs this item while the menu is closed, and the hint drawn right-aligned in its row. null removes it. Set it before the menu is shown: an open cascade measured its rows against the hint it had then, and a change is not picked up until it is rebuilt.
      Throws:
      IllegalStateException - on a submenu item or a separator. No platform gives a submenu a shortcut, because a shortcut runs a command and opening a submenu is not one; a shortcut on the items inside it is what was meant.
    • mnemonic

      public char mnemonic()
      The access letter, uppercased, or 0 when none is declared: the character underlined in the row and the one that chooses this item while its menu is open.
    • setMnemonic

      public MenuItem setMnemonic(char value)
      Declares the access letter: it is underlined in the row, and pressing it while this item's menu is open chooses the item (opens it, for a submenu). Matched case-insensitively, and only against an enabled item: a disabled row is not reachable by its letter any more than by its arrow keys.

      The letter does not have to occur in the label; when it does not, nothing is underlined and the key still works.

      Parameters:
      value - a letter or digit; 0 removes the mnemonic
      Throws:
      IllegalArgumentException - for any other character (there is nothing to underline and no key code to match)
      IllegalStateException - on a separator
    • mnemonicIndex

      public int mnemonicIndex()
      Index into label() of the character to underline (the first case-insensitive occurrence of the mnemonic), or -1 when there is no mnemonic, no label, or the letter does not occur in it. Recomputed from the current label, so it follows a label that changed with the UI language.