Java Code Examples for javax.swing.JMenuItem#setAccelerator()

The following examples show how to use javax.swing.JMenuItem#setAccelerator() . 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: MenuSimulate.java    From Logisim with GNU General Public License v3.0 6 votes vote down vote up
private void recreateStateMenu(JMenu menu, ArrayList<CircuitStateMenuItem> items, int code) {
	menu.removeAll();
	menu.setEnabled(items.size() > 0);
	boolean first = true;
	int mask = (Main.JAVA_VERSION < 10.0) ? 128 : getToolkit().getMenuShortcutKeyMaskEx();
	for (int i = items.size() - 1; i >= 0; i--) {
		JMenuItem item = items.get(i);
		menu.add(item);
		if (first) {
			item.setAccelerator(KeyStroke.getKeyStroke(code, mask));
			first = false;
		} else {
			item.setAccelerator(null);
		}
	}
}
 
Example 2
Source File: QFixMessengerFrame.java    From quickfix-messenger with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private void initHelpMenu()
{
	helpMenu = new JMenu("Help");
	helpMenu.setMnemonic('H');

	JMenuItem helpMenuItem = new JMenuItem("Help");
	helpMenuItem.setIcon(IconBuilder.build(getMessenger().getConfig(),
			IconBuilder.HELP_ICON));
	helpMenuItem.setMnemonic('H');
	helpMenuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F2,
			InputEvent.SHIFT_DOWN_MASK));
	helpMenuItem.addActionListener(new HelpActionListener(this));

	JMenuItem aboutMenuItem = new JMenuItem("About");
	aboutMenuItem.setMnemonic('A');
	aboutMenuItem.addActionListener(new AboutActionListener(this));

	helpMenu.add(helpMenuItem);
	helpMenu.add(aboutMenuItem);
}
 
Example 3
Source File: EditorFrame.java    From settlers-remake with MIT License 6 votes vote down vote up
/**
 * Create a menu item for a specific action
 * 
 * @param action
 *            The action
 * @param menuActionName
 *            The name of the action
 * @param menu
 *            The menu to add the action
 */
private void createMenuItemForAction(final Action action, String menuActionName, JMenu menu) {
	final JMenuItem it;
	Boolean displayAsCheckbox = (Boolean) action.getValue(EditorFrame.DISPLAY_CHECKBOX);
	if (displayAsCheckbox != null && displayAsCheckbox) {
		it = createCheckboxMenuItemForAction(action);
		menu.add(it);
	} else {
		it = menu.add(action);
	}

	action.addPropertyChangeListener(evt -> {
		if (Action.NAME.equals(evt.getPropertyName())) {
			it.setText((String) evt.getNewValue());
		}
	});
	it.setText((String) action.getValue(Action.NAME));

	String shortcut = this.shortcut.getProperty(menuActionName);
	if (shortcut != null) {
		it.setAccelerator(
				KeyStroke.getKeyStroke(shortcut));
	}
}
 
Example 4
Source File: Annotations.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addAcceleretors(Action a, JMenuItem item, BaseKit kit){
    // Try to get the accelerator
    javax.swing.text.JTextComponent target = Utilities.getFocusedComponent();
    if (target == null) return;
    javax.swing.text.Keymap km = target.getKeymap();
    if (km != null) {
        javax.swing.KeyStroke[] keys = km.getKeyStrokesForAction(a);
        if (keys != null && keys.length > 0) {
            item.setAccelerator(keys[0]);
        }else{
            // retrieve via actionName
            String actionName = (String)a.getValue(Action.NAME);
            if (actionName == null) return;
            BaseAction action = (BaseAction)kit.getActionByName(actionName);
            if (action == null) return;
            keys = km.getKeyStrokesForAction(action);
            if (keys != null && keys.length > 0) {
                item.setAccelerator(keys[0]);
            }                        
        }
    }
}
 
Example 5
Source File: ObjectPopupMenu.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private void setCCP() {
    TransferActionListener actionListener = new TransferActionListener();
    cut = new JMenuItem("Cut");
    cut.setActionCommand((String) TransferHandler.getCutAction().getValue(Action.NAME));
    cut.addActionListener(actionListener);
    cut.setAccelerator(Keystroke.CUT);
    cut.setMnemonic(KeyEvent.VK_T);
    add(cut);

    copy = new JMenuItem("Copy");
    copy.setActionCommand((String) TransferHandler.getCopyAction().getValue(Action.NAME));
    copy.addActionListener(actionListener);
    copy.setAccelerator(Keystroke.COPY);
    copy.setMnemonic(KeyEvent.VK_C);
    add(copy);

    paste = new JMenuItem("Paste");
    paste.setActionCommand((String) TransferHandler.getPasteAction().getValue(Action.NAME));
    paste.addActionListener(actionListener);
    paste.setAccelerator(Keystroke.PASTE);
    paste.setMnemonic(KeyEvent.VK_P);
    add(paste);
}
 
Example 6
Source File: JavaKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addAcceleretors(Action a, JMenuItem item, JTextComponent target){
    // Try to get the accelerator
    Keymap km = target.getKeymap();
    if (km != null) {

        KeyStroke[] keys = km.getKeyStrokesForAction(a);
        if (keys != null && keys.length > 0) {
            item.setAccelerator(keys[0]);
        }else if (a!=null){
            KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY);
            if (ks!=null) {
                item.setAccelerator(ks);
            }
        }
    }
}
 
Example 7
Source File: AbstractMenuCreator.java    From wpcleaner with Apache License 2.0 6 votes vote down vote up
/**
 * Add an item to a menu.
 * 
 * @param menu Menu.
 * @param page Page.
 * @param title Element title (if null, the page title will be used).
 * @param asIs True if the message should be used as is (no mnemonic).
 * @param action Action.
 * @param accelerator Accelerator.
 * @return Number of items added.
 */
public int addItem(
    JPopupMenu menu, Page page, String title, boolean asIs,
    ActionListener action, KeyStroke accelerator) {
  if (menu == null) {
    return 0;
  }
  if ((title == null) && ((page == null) || (page.getTitle() == null))) {
    return 0;
  }
  JMenuItem menuItem = Utilities.createJMenuItem(title != null ? title : page.getTitle(), asIs);
  if (page != null) {
    updateFont(menuItem, page);
  }
  if (action != null) {
    menuItem.addActionListener(action);
  }
  if (accelerator != null) {
    menuItem.setAccelerator(accelerator);
  }
  menu.add(menuItem);
  return 1;
}
 
Example 8
Source File: AnimationEditor.java    From 3Dscript with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public JMenuItem addToMenu(final JMenu menu, final String menuEntry,
	final int key, final int modifiers)
{
	final JMenuItem item = new JMenuItem(menuEntry);
	menu.add(item);
	if (key != 0) item.setAccelerator(KeyStroke.getKeyStroke(key, modifiers));
	item.addActionListener(this);
	return item;
}
 
Example 9
Source File: VizDesktop.java    From ontopia with Apache License 2.0 5 votes vote down vote up
protected void addRDBMSImportMenuItem(JMenu containingMenu) {
  JMenuItem mItem = new JMenuItem(Messages
      .getString("Viz.MenuOpenRDBMSTopicMap"));
  mItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent anE) {
      menuOpenRDBMSTopicMap();
    }
  });
  mItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
      KeyInputManager.KEY_MASK));
  containingMenu.add(mItem);
}
 
Example 10
Source File: GameMenu.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private void addEditMode() {
  final JCheckBoxMenuItem editMode = new JCheckBoxMenuItem("Enable Edit Mode");
  editMode.setModel(frame.getEditModeButtonModel());
  final JMenuItem editMenuItem = add(editMode);
  editMenuItem.setMnemonic(KeyEvent.VK_E);
  editMenuItem.setAccelerator(
      KeyStroke.getKeyStroke(
          KeyEvent.VK_E, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
}
 
Example 11
Source File: MainFrameClassicMenu.java    From jpexs-decompiler with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void addMenuItem(String path, String title, String icon, ActionListener action, int priority, final ActionListener subLoader, boolean isLeaf, HotKey key, boolean isOptional) {
    path = mapping(path);

    menuHotkeys.put(path, key);
    menuActions.put(path, action);

    if (!isLeaf) {
        //action is ignored
        addMenu(path, title, icon, subLoader);
        return;
    }
    if (path.startsWith("_/")) {
        return;
    }
    String parentPath = "";
    if (path.contains("/")) {
        parentPath = path.substring(0, path.lastIndexOf('/'));
    }
    MenuElement parentMenu = menuElements.get(parentPath);
    if (parentMenu == null) {
        throw new IllegalArgumentException("Parent menu " + path + " does not exist");
    }
    JMenuItem menuItem = new JMenuItem(title);
    if (icon != null) {
        menuItem.setIcon(View.getIcon(icon, 16));
    }
    if (action != null) {
        menuItem.addActionListener(action);
    }
    if (key != null) {
        menuItem.setAccelerator(KeyStroke.getKeyStroke(key.key, key.getModifier()));
    }
    if (parentMenu instanceof JMenu) {
        ((JMenu) parentMenu).add(menuItem);
    } else {
        throw new IllegalArgumentException("Parent path " + path + " is not a menu");
    }
    menuElements.put(path, menuItem);
}
 
Example 12
Source File: CompletionActionsMainMenu.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Sets the state of JMenuItem*/
protected @Override void setMenu(){
    
    ActionMap am = getContextActionMap();
    Action action = null;
    if (am != null) {
        action = am.get(getActionName());
    }
    
    JMenuItem presenter = getMenuPresenter();
    Action presenterAction = presenter.getAction();
    if (presenterAction == null){
        presenter.setAction(this);
        presenter.setToolTipText(null); /* bugfix #62872 */ 
        menuInitialized = false;
    } 
    else {
        if (!this.equals(presenterAction)){
            presenter.setAction(this);
            presenter.setToolTipText(null); /* bugfix #62872 */
            menuInitialized = false;
        }
    }

    if (!menuInitialized){
        Mnemonics.setLocalizedText(presenter, getMenuItemText());
        menuInitialized = true;
    }

    presenter.setEnabled(action != null);
    JTextComponent comp = Utilities.getFocusedComponent();
    if (comp != null && comp instanceof JEditorPane){
        addAccelerators(this, presenter, comp);
    } else {
        presenter.setAccelerator(getDefaultAccelerator());
    }

}
 
Example 13
Source File: FileMenu.java    From triplea with GNU General Public License v3.0 5 votes vote down vote up
private void addExitMenu() {
  final boolean isMac = SystemProperties.isMac();
  final JMenuItem leaveGameMenuExit =
      new JMenuItem(SwingAction.of("Leave Game", e -> frame.leaveGame()));
  leaveGameMenuExit.setMnemonic(KeyEvent.VK_L);
  if (isMac) { // On Mac OS X, the command-Q is reserved for the Quit action,
    // so set the command-L key combo for the Leave Game action
    leaveGameMenuExit.setAccelerator(
        KeyStroke.getKeyStroke(
            KeyEvent.VK_L, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
  } else { // On non-Mac operating systems, set the Ctrl-Q key combo for the Leave Game action
    leaveGameMenuExit.setAccelerator(
        KeyStroke.getKeyStroke(
            KeyEvent.VK_Q, Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx()));
  }
  add(leaveGameMenuExit);

  // Mac OS X automatically creates a Quit menu item under the TripleA menu,
  // so all we need to do is register that menu item with triplea's shutdown mechanism
  if (isMac) {
    MacOsIntegration.setQuitHandler(frame);
  } else { // On non-Mac operating systems, we need to manually create an Exit menu item
    final JMenuItem menuFileExit =
        new JMenuItem(SwingAction.of("Exit Program", e -> frame.shutdown()));
    menuFileExit.setMnemonic(KeyEvent.VK_E);
    add(menuFileExit);
  }
}
 
Example 14
Source File: StringInterpolator.java    From GIFKR with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void createMenuBar() {
	JMenuItem copy = new JMenuItem("Copy");
	copy.addActionListener(ae -> TransferableUtils.copyObject(Keyframe.deepCopy(keyframes)));
	copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.META_MASK));
	
	JMenuItem paste = new JMenuItem("Paste");
	paste.addActionListener(ae -> {
		Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(this);
		try {
			
			ArrayList<Keyframe<Serializable>> data = (ArrayList<Keyframe<Serializable>>) t.getTransferData(TransferableUtils.objectDataFlavor);
			if(data.isEmpty())
				return;
			//Yes this is ugly. There is really no better way of doing it though. Forgive me
			if(data.get(0).getValue() instanceof String)
				keyframes = (ArrayList<Keyframe<String>>) (Object) data;

			//keyframes = (ArrayList<Keyframe<String>>) t.getTransferData(TransferableUtils.objectDataFlavor);
			refresh();
		} catch (UnsupportedFlavorException | IOException e1) {
			System.err.println("Invalid data copied");
		}
	});
	paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.META_MASK));
	
	JMenuBar bar = new JMenuBar();
	JMenu edit = new JMenu("Edit");
	edit.add(copy);
	edit.add(paste);
	bar.add(edit);
	setJMenuBar(bar);
}
 
Example 15
Source File: OurUtil.java    From org.alloytools.alloy with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a new JMenuItem then add it to an existing JMenu.
 *
 * @param parent - the JMenu to add this JMenuItem into (or null if you don't
 *            want to add it to any JMenu yet)
 * @param label - the text to show on the menu
 * @param attrs - a list of attributes to apply onto the new JMenuItem
 *            <p>
 *            If one positive number a is supplied, we call setMnemonic(a)
 *            <p>
 *            If two positive numbers a and b are supplied, and a!=VK_ALT, and
 *            a!=VK_SHIFT, we call setMnemoic(a) and setAccelerator(b)
 *            <p>
 *            If two positive numbers a and b are supplied, and a==VK_ALT or
 *            a==VK_SHIFT, we call setAccelerator(a | b)
 *            <p>
 *            If an ActionListener is supplied, we call addActionListener(x)
 *            <p>
 *            If an Boolean x is supplied, we call setEnabled(x)
 *            <p>
 *            If an Icon x is supplied, we call setIcon(x)
 */
public static JMenuItem menuItem(JMenu parent, String label, Object... attrs) {
    JMenuItem m = new JMenuItem(label, null);
    int accelMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    boolean hasMnemonic = false;
    for (Object x : attrs) {
        if (x instanceof Character || x instanceof Integer) {
            int k = (x instanceof Character) ? ((int) ((Character) x)) : ((Integer) x).intValue();
            if (k < 0)
                continue;
            if (k == KeyEvent.VK_ALT) {
                hasMnemonic = true;
                accelMask = accelMask | InputEvent.ALT_MASK;
                continue;
            }
            if (k == KeyEvent.VK_SHIFT) {
                hasMnemonic = true;
                accelMask = accelMask | InputEvent.SHIFT_MASK;
                continue;
            }
            if (!hasMnemonic) {
                m.setMnemonic(k);
                hasMnemonic = true;
            } else
                m.setAccelerator(KeyStroke.getKeyStroke(k, accelMask));
        }
        if (x instanceof ActionListener)
            m.addActionListener((ActionListener) x);
        if (x instanceof Icon)
            m.setIcon((Icon) x);
        if (x instanceof Boolean)
            m.setEnabled((Boolean) x);
    }
    if (parent != null)
        parent.add(m);
    return m;
}
 
Example 16
Source File: GUIHistory.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
private JMenuItem createMenuItem(String name, int key, KeyStroke hotkey, ActionListener l) {
	JMenuItem out = new JMenuItem (name);
	if (key >= 0) {
		out.setMnemonic(key);
	}
	if (hotkey != null) {
		out.setAccelerator(hotkey);
	}
	out.addActionListener(l);
	return out;
}
 
Example 17
Source File: Test6501431.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
protected JMenuItem getObject() {
    JMenuItem menu = new JMenuItem();
    menu.setAccelerator(KeyStroke.getKeyStroke("ctrl F2"));
    return menu;
}
 
Example 18
Source File: Test6501431.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
protected JMenuItem getObject() {
    JMenuItem menu = new JMenuItem();
    menu.setAccelerator(KeyStroke.getKeyStroke("ctrl F2"));
    return menu;
}
 
Example 19
Source File: Test6501431.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
protected JMenuItem getObject() {
    JMenuItem menu = new JMenuItem();
    menu.setAccelerator(KeyStroke.getKeyStroke("ctrl F2"));
    return menu;
}
 
Example 20
Source File: Test6501431.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
protected JMenuItem getObject() {
    JMenuItem menu = new JMenuItem();
    menu.setAccelerator(KeyStroke.getKeyStroke("ctrl F2"));
    return menu;
}