Package limn.components
Class MenuItem
java.lang.Object
limn.components.MenuItem
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 Summary
Modifier and TypeMethodDescriptionThe keyboard shortcut shown at the right of the row and dispatched while closed, or null.static MenuItemA checkable item showing a check mark for its state; choosing it flips the state and callsonTogglewith the new value.static MenuItemcheck(I18nString label, boolean checked, Consumer<Boolean> onToggle) A checkable item whose label follows the UI language.booleanbooleanWhether the item can be chosen; a disabled one still occupies a row.label()The label as it currently reads, ornullfor a separator.The localizable value behindlabel();nullfor a separator.charmnemonic()The access letter, uppercased, or0when none is declared: the character underlined in the row and the one that chooses this item while its menu is open.intIndex intolabel()of the character to underline (the first case-insensitive occurrence of the mnemonic), or-1when there is no mnemonic, no label, or the letter does not occur in it.static MenuItemA command that runsactionwhen chosen.static MenuItemof(I18nString label, Runnable action) A command whose label follows the UI language; seeI18nString.static MenuItemA non-interactive divider line between groups of items.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.setChecked(boolean value) Sets a check item's state (does not fireonToggle).setEnabled(boolean value) Enables or disables the item.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).static MenuItemAn item that opens a nestedsubmenuon hover / right-arrow.static MenuItemsubmenu(I18nString label, Menu submenu) A submenu item whose label follows the UI language.
-
Method Details
-
of
A command that runsactionwhen chosen. -
of
A command whose label follows the UI language; seeI18nString. -
check
A checkable item showing a check mark for its state; choosing it flips the state and callsonTogglewith the new value. -
check
A checkable item whose label follows the UI language. -
separator
A non-interactive divider line between groups of items. -
label
The label as it currently reads, ornullfor a separator. -
labelSource
The localizable value behindlabel();nullfor a separator. -
isEnabled
public boolean isEnabled()Whether the item can be chosen; a disabled one still occupies a row. -
setEnabled
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
Sets a check item's state (does not fireonToggle). UI thread when displayed. -
accelerator
The keyboard shortcut shown at the right of the row and dispatched while closed, or null. -
setAccelerator
Declares the keyboard shortcut that runs this item while the menu is closed, and the hint drawn right-aligned in its row.nullremoves 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, or0when none is declared: the character underlined in the row and the one that chooses this item while its menu is open. -
setMnemonic
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;0removes 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 intolabel()of the character to underline (the first case-insensitive occurrence of the mnemonic), or-1when 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.
-