Java Code Examples for java.awt.MenuItem#setShortcut()

The following examples show how to use java.awt.MenuItem#setShortcut() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseManager.java    From evosql with Apache License 2.0 5 votes vote down vote up
void addMenuItems(Menu f, String[] m) {

        for (int i = 0; i < m.length; i++) {
            MenuItem item = new MenuItem(m[i].substring(1));
            char     c    = m[i].charAt(0);

            if (c != '-') {
                item.setShortcut(new MenuShortcut(c));
            }

            item.addActionListener(this);
            f.add(item);
        }
    }
 
Example 2
Source File: GUI.java    From CQL with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("deprecation")
	private static Menu makeFileMenu() {
		Menu fileMenu = new Menu("File");

		MenuItem openItem = new MenuItem("Open");
		MenuItem saveItem = new MenuItem("Save");
		MenuItem saveAsItem = new MenuItem("Save As");
		MenuItem saveAllItem = new MenuItem("Save All");
		MenuItem closeItem = new MenuItem("Close");
		MenuItem exitItem = new MenuItem("Quit");

		Map<Language, MenuItem> newItems = new THashMap<>();
		for (Language l : Language.values0()) {
			if (l.toString().contains(" ")) {
				continue;
			}
			if (l.toString().contains("EASIK")) {
				continue;
			}
			MenuItem newItem = new MenuItem("New " + l);
			fileMenu.add(newItem);
			newItems.put(l, newItem);
			newItem.addActionListener((ActionEvent e) -> newAction(null, "", l));
		}

		fileMenu.add(openItem);
		// fileMenu.add(openItem2);
		fileMenu.add(saveItem);
		fileMenu.add(saveAsItem);
		fileMenu.add(saveAllItem);
		fileMenu.add(closeItem);
		fileMenu.add(exitItem);

		closeItem.addActionListener(e -> closeAction());
		saveAsItem.addActionListener(e -> saveAsActionAlternate(getSelectedEditor()));

		KeyStroke ctrlS = KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut s = new MenuShortcut(ctrlS.getKeyCode());
		saveItem.setShortcut(s);

		// TODO aql why doesn't shift mask work? order?
		MenuShortcut sA = new MenuShortcut(ctrlS.getKeyCode(), true);
		saveAllItem.setShortcut(sA);
		saveAllItem.addActionListener(e -> saveAllAction());

		KeyStroke ctrlQ = KeyStroke.getKeyStroke(KeyEvent.VK_Q, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut q = new MenuShortcut(ctrlQ.getKeyCode());
		exitItem.setShortcut(q);

		KeyStroke ctrlW = KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
//		KeyStroke commandW = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.META_DOWN_MASK);
		MenuShortcut c = new MenuShortcut(ctrlW.getKeyCode());
		closeItem.setShortcut(c);

		openItem.addActionListener(e -> openActionAlternate());

		saveItem.addActionListener(e -> saveAction());

		exitItem.addActionListener(e -> exitAction());

		KeyStroke ctrlN = KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut n = new MenuShortcut(ctrlN.getKeyCode());
		newItems.get(Language.getDefault()).setShortcut(n);
		KeyStroke ctrlO = KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
		MenuShortcut o = new MenuShortcut(ctrlO.getKeyCode());
		openItem.setShortcut(o);
		return fileMenu;

	}