Shortcuts
De Wiki
It exists two kind of methods, corresponding to different needs :
- setShortcut(), useful to replace a mouse click
- setAccelerator(), used to directly go to a functionnality
Let us explain it with the following example where we will have a File Menu with sub items to Load or Save a file.
Using setShortcut() will allow to replace the mouse clicks as is:
- ALT+F to access to the File Menu
- ALT+L to access to the Load item
- ALT+S to access to the Save item
So, to load a file, you will have to type ALT+F then ALT+L (as you would have been to click two times using the mouse).
Using setAccelerator() will allow to access directly to a functionnality (it is actually a shortcut):
- CTRL+L to access directly to the Load item
- CTRL+S to access directly to the Save item
Here is how to proceed ...
// FileMenu initialization fileMenu = new GMenu("File"); fileMenu.getJMenu().setMnemonic(KeyEvent.VK_F); // ALT+F to access to this menu // Moad Item initialization itemLoad = new GMenuItem("Load ..."); // ALT+L to access to it when File menu is active itemLoad.setShortcut(KeyEvent.VK_L); // CTRL+L to access directly to it itemLoad.setAccelerator(KeyEvent.VK_L, ActionEvent.CTRL_MASK); fileMenu.add(itemLoad); itemSave = new GMenuItem("Save ..."); // ALT+S to access to it when File menu is active itemSave.setShortCut(KeyEvent.VK_S); // CTRL+S to access directly to it itemSave.setAccelerator(KeyEvent.VK_S, ActionEvent.CTRL_MASK); fileMenu.add(itemSave);