Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you make menu item (JMenuItem) shortcut?

Tags:

So i noticed that in awt there is a MenuItem constructor for adding a CTRL + (some key) shortcut, but there is no such constructor for JMenuItem. What is the correct way to do this?

I need an equivelent of awt:

MenuItem mi = new MenuItem("Copy", new MenuShortcut(KeyEvent.VK_C)); 

but for Swing.

like image 850
Adrian Hristov Avatar asked Nov 13 '12 18:11

Adrian Hristov


1 Answers

Example for CTRL + N.

menuItem.setAccelerator(KeyStroke.getKeyStroke('N', Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx())); 

Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx() returns control key (ctrl) on Windows and linux, and command key (⌘) on Mac OS.

like image 116
Dan D. Avatar answered Oct 13 '22 08:10

Dan D.