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

The following examples show how to use javax.swing.JMenuItem#setAction() . 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: POMModelVisitor.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@Messages({"ACT_Show=Show in POM", 
    "# {0} - artifactid of a project",
    "ACT_Current=Current: {0}",
    "# {0} - artifactid of a project",
    "ACT_PARENT=Parent: {0}"})
public JMenuItem getPopupPresenter() {
    JMenu menu = new JMenu();
    menu.setText(ACT_Show());
    POMCutHolder pch = node.getLookup().lookup(POMCutHolder.class);
    POMModel[] mdls = pch.getSource();
    Object[] val = pch.getCutValues();
    int index = 0;
    for (POMModel mdl : mdls) {
        String artifact = mdl.getProject().getArtifactId();
        JMenuItem item = new JMenuItem();
        item.setAction(new SelectAction(node, index));
        if (index == 0) {
            item.setText(ACT_Current(artifact != null ? artifact : "project"));
        } else {
            item.setText(ACT_PARENT(artifact != null ? artifact : "project"));
        }
        item.setEnabled(/* #199345 */index < val.length && val[index] != null);
        menu.add(item);
        index++;
    }
    return menu;
}
 
Example 2
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 3
Source File: MainFrame.java    From SVG-Android with Apache License 2.0 5 votes vote down vote up
private void buildMenuBar() {
    JMenu fileMenu = new JMenu(R.string.file);
    fileMenu.setMnemonic('F');
    JMenuItem openMenuItem = new JMenuItem();
    mSaveMenuItem = new JMenuItem();
    JMenuItem exitMenuItem = new JMenuItem();

    openMenuItem.setAction(mActionsMap.get(OpenAction.ACTION_NAME));
    fileMenu.add(openMenuItem);

    mSaveMenuItem.setAction(mActionsMap.get(SaveAction.ACTION_NAME));
    mSaveMenuItem.setEnabled(false);
    fileMenu.add(mSaveMenuItem);

    exitMenuItem.setAction(mActionsMap.get(ExitAction.ACTION_NAME));
    fileMenu.add(exitMenuItem);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);

    JMenu helpMenu = new JMenu(R.string.help);
    JMenuItem aboutItem = new JMenuItem(R.string.about);
    aboutItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(getContentPane(), R.string.about_text);
        }
    });
    helpMenu.add(aboutItem);
    menuBar.add(helpMenu);
    setJMenuBar(menuBar);
}
 
Example 4
Source File: SwingSet3.java    From littleluck with Apache License 2.0 5 votes vote down vote up
protected JMenuBar createMenuBar() {

    JMenuBar menubar = new JMenuBar();
    menubar.setName("menubar");
    
    // File menu
    JMenu fileMenu = new JMenu();
    fileMenu.setName("file");
    menubar.add(fileMenu);
    
    // File -> Quit
    if (!runningOnMac()) {
        JMenuItem quitItem = new LuckMenuItem();
        quitItem.setOpaque(true);
        quitItem.setName("quit");
        quitItem.setAction(getAction("quit"));
        fileMenu.add(quitItem);
    }
   
    // View menu
    JMenu viewMenu = new JMenu();
    viewMenu.setName("view");
    // View -> Look and Feel       
    viewMenu.add(createLookAndFeelMenu());
    // View -> Source Code Visible
    sourceCodeCheckboxItem = new LuckCheckBoxMenuItem();
    sourceCodeCheckboxItem.setSelected(isSourceCodeVisible());
    sourceCodeCheckboxItem.setName("sourceCodeCheckboxItem");
    sourceCodeCheckboxItem.addChangeListener(new SourceVisibilityChangeListener());
    viewMenu.add(sourceCodeCheckboxItem);
    menubar.add(viewMenu);

    return menubar;
}
 
Example 5
Source File: SwingSet3.java    From beautyeye with Apache License 2.0 5 votes vote down vote up
protected JMenuBar createMenuBar() {

    JMenuBar menubar = new JMenuBar();
    menubar.setName("menubar");
    
    // File menu
    JMenu fileMenu = new JMenu();
    fileMenu.setName("file");
    menubar.add(fileMenu);
    
    // File -> Quit
    if (!runningOnMac()) {
        JMenuItem quitItem = new JMenuItem();
        quitItem.setName("quit");
        quitItem.setAction(getAction("quit"));
        fileMenu.add(quitItem);
    }
   
    // View menu
    JMenu viewMenu = new JMenu();
    viewMenu.setName("view");
    // View -> Look and Feel       
    viewMenu.add(createLookAndFeelMenu());
    // View -> Source Code Visible
    sourceCodeCheckboxItem = new JCheckBoxMenuItem();
    sourceCodeCheckboxItem.setSelected(isSourceCodeVisible());
    sourceCodeCheckboxItem.setName("sourceCodeCheckboxItem");
    sourceCodeCheckboxItem.addChangeListener(new SourceVisibilityChangeListener());
    viewMenu.add(sourceCodeCheckboxItem);
    menubar.add(viewMenu);

    return menubar;
}
 
Example 6
Source File: ComponentSource.java    From LoboBrowser with MIT License 5 votes vote down vote up
static JMenuItem menuItem(final String title, final char mnemonic, final KeyStroke accelerator, final Action action) {
  final JMenuItem item = new JMenuItem();
  item.setAction(action);
  item.setText(title);
  if (mnemonic != 0) {
    item.setMnemonic(mnemonic);
  }
  if (accelerator != null) {
    item.setAccelerator(accelerator);
  }
  return item;
}
 
Example 7
Source File: ComponentSource.java    From LoboBrowser with MIT License 5 votes vote down vote up
private JMenuItem navMenuItem(final String title, final String toolTip, final String urlPrefix, final boolean urlEncode) {
  final JMenuItem item = new JMenuItem();
  item.setAction(this.actionPool.addUrlPrefixNavigateAction(urlPrefix, urlEncode));
  item.setText(title);
  item.setToolTipText(toolTip);
  return item;
}
 
Example 8
Source File: StandardEditingPopupMenu.java    From importer-exporter with Apache License 2.0 5 votes vote down vote up
public void init(Component component) {
	cut = new JMenuItem();
	copy = new JMenuItem();		
	paste = new JMenuItem();
	selectAll = new JMenuItem();

	cut.setActionCommand((String)TransferHandler.getCutAction().getValue(Action.NAME));
	copy.setActionCommand((String)TransferHandler.getCopyAction().getValue(Action.NAME));
	paste.setActionCommand((String)TransferHandler.getPasteAction().getValue(Action.NAME));

	cut.addActionListener(new TransferActionListener());
	copy.addActionListener(new TransferActionListener());
	paste.addActionListener(new TransferActionListener());

	if (component instanceof JTextComponent) {
		selectAll.setAction(new TextSelectAllAction());

		if (component instanceof JPasswordField) {
			cut.setEnabled(false);
			copy.setEnabled(false);
		}
	} else if (component instanceof JList)
		selectAll.setAction(new ListSelectAllAction());		

	cut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
	copy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
	paste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
	selectAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));

	add(cut);
	add(copy);
	add(paste);
	addSeparator();
	add(selectAll);
}
 
Example 9
Source File: ActionManager.java    From audiveris with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void registerDomainActions (String domain,
                                    JMenu menu)
{
    // Create all type sections for this menu
    for (int section : Actions.getSections()) {
        logger.debug("Starting section: {}", section);

        // Use a separator between sections
        menu.addSeparator();

        for (ActionDescriptor desc : Actions.getAllDescriptors()) {
            if (desc.domain.equalsIgnoreCase(domain) && (desc.section == section)) {
                // Skip advanced topics, unless explicitly set
                if ((desc.topic != null) && !desc.topic.isSet()) {
                    continue;
                }

                logger.debug("Registering {}", desc);

                try {
                    // Allocate menu item of proper class
                    final Class<? extends JMenuItem> itemClass;

                    if (desc.itemClassName != null) {
                        itemClass = (Class<? extends JMenuItem>) classLoader.loadClass(
                                desc.itemClassName);
                    } else if (desc.menuName != null) {
                        itemClass = JMenu.class;
                    } else {
                        itemClass = JMenuItem.class;
                    }

                    JMenuItem item = itemClass.newInstance();

                    // Inject menu item information
                    if (desc.methodName != null) {
                        item.setText(desc.methodName); // As default

                        ApplicationAction action = registerAction(desc);

                        if (action != null) {
                            action.setSelected(action.isSelected());
                            item.setAction(action);
                            menu.add(item);
                        } else {
                            logger.warn("Could not register {}", desc);
                        }
                    } else if (desc.menuName != null) {
                        item.setText(desc.menuName); // As default
                        item.setName(desc.menuName);
                        menu.add(item);
                    }
                } catch (ClassNotFoundException |
                         IllegalAccessException |
                         InstantiationException ex) {
                    logger.warn("Error with " + desc.itemClassName, ex);
                }
            }
        }
    }
}
 
Example 10
Source File: ActionManager.java    From libreveris with GNU Lesser General Public License v3.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void registerDomainActions (String domain,
                                    JMenu menu)
{
    // Create all type sections for this menu
    for (int section : Actions.getSections()) {
        logger.debug("Starting section: {}", section);

        // Use a separator between sections
        menu.addSeparator();

        for (ActionDescriptor desc : Actions.getAllDescriptors()) {
            if (desc.domain.equalsIgnoreCase(domain)
                && (desc.section == section)) {
                logger.debug("Registering {}", desc);

                try {
                    Class<? extends JMenuItem> itemClass;

                    if (desc.itemClassName != null) {
                        itemClass = (Class<? extends JMenuItem>) classLoader.
                                loadClass(
                                desc.itemClassName);
                    } else {
                        itemClass = JMenuItem.class;
                    }

                    JMenuItem item = itemClass.newInstance();
                    item.setText(desc.methodName);

                    ApplicationAction action = registerAction(desc);

                    if (action != null) {
                        action.setSelected(action.isSelected());
                        item.setAction(action);
                        menu.add(item);
                    } else {
                        logger.warn("Could not register {}", desc);
                    }
                } catch (ClassNotFoundException | InstantiationException |
                        IllegalAccessException ex) {
                    logger.warn("Error with " + desc.itemClassName, ex);
                }
            }
        }
    }
}