to @Pete
you can combine any non_chars accelerators, but itβs not possible for keys in the range [az]
&& & & [0-9]
for the accelerator JMenu (Item) you can use
KeyEvent
or Character.valueOf('char')
for characters [az]
&& & & [0-9]
and as the second parameter
Event
or ActionEvent
or InputEvent
, note that each of the APIs implements different keyboard maps
can combine KeyStroke, but with bitwise | or, but returns weird KeyStrokes
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.*; import javax.swing.border.BevelBorder; public class MenuExample extends JPanel { private static final long serialVersionUID = 1L; private Icon errorIcon = UIManager.getIcon("OptionPane.errorIcon"); private Icon infoIcon = UIManager.getIcon("OptionPane.informationIcon"); private Icon warnIcon = UIManager.getIcon("OptionPane.warningIcon"); private Icon questIcon = UIManager.getIcon("OptionPane.questionIcon"); private JTextPane pane; private JMenuBar menuBar; public MenuExample() { menuBar = new JMenuBar(); JMenu formatMenu = new JMenu("Justify"); formatMenu.setMnemonic('J'); MenuAction leftJustifyAction = new MenuAction("Left", errorIcon); MenuAction rightJustifyAction = new MenuAction("Right", infoIcon); MenuAction centerJustifyAction = new MenuAction("Center", warnIcon); MenuAction fullJustifyAction = new MenuAction("Full", questIcon); JMenuItem item; item = formatMenu.add(leftJustifyAction); item.setMnemonic('L'); item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.ALT_MASK)); item = formatMenu.add(rightJustifyAction); item.setMnemonic('R'); item.setAccelerator(KeyStroke.getKeyStroke(ActionEvent.CTRL_MASK | KeyEvent.VK_N, ActionEvent.CTRL_MASK & KeyEvent.VK_B));
source share