Java Code Examples for javax.swing.JMenuItem

The following examples show how to use javax.swing.JMenuItem. These examples are extracted from open source projects. 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
/**
 * Pushes menu.
 *
 * @param choosers Array of choosers to find menuItems to push.
 * @return Last pushed JMenuItem.
 * @throws TimeoutExpiredException
 */
public JMenuItem pushMenu(final ComponentChooser[] choosers) {
    return ((JMenuItem) produceTimeRestricted(new Action<Object, Void>() {
        @Override
        public Object launch(Void obj) {
            //TDB 1.5 menu workaround
            getQueueTool().waitEmpty();
            Object result = driver.pushMenu(JPopupMenuOperator.this,
                    JMenuOperator.converChoosers(choosers));
            getQueueTool().waitEmpty();
            return result;
        }

        @Override
        public String getDescription() {
            return JMenuOperator.createDescription(choosers);
        }

        @Override
        public String toString() {
            return "JPopupMenuOperator.pushMenu.ComponentChooser{description = " + getDescription() + '}';
        }
    }, "JMenuOperator.PushMenuTimeout"));
}
 
Example 2
Source Project: freecol   Source File: QuickActionMenu.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Add an item to pay arrears on the given goods type.
 *
 * @param goodsType The {@code GoodsType} to pay arrears on.
 */
private void addPayArrears(final GoodsType goodsType) {
    final InGameController igc = freeColClient.getInGameController();

    JMenuItem menuItem = Utility.localizedMenuItem("payArrears");
    menuItem.addActionListener((ActionEvent ae) -> {
            igc.payArrears(goodsType);
            // FIXME: fix pcls so this hackery can go away
            if (parentPanel instanceof CargoPanel) {
                CargoPanel cargoPanel = (CargoPanel) parentPanel;
                cargoPanel.initialize();
            }
            parentPanel.revalidate();
        });
    this.add(menuItem);
}
 
Example 3
Source Project: netbeans   Source File: NbEditorKit.java    License: Apache License 2.0 6 votes vote down vote up
public @Override JMenuItem getPopupMenuItem(JTextComponent target) {
    String menuText = org.openide.util.NbBundle.getBundle (NbEditorKit.class).
        getString("Menu/View/CodeFolds");
    JMenu menu = new JMenu(menuText);
    Mnemonics.setLocalizedText(menu, menuText);
    setAddSeparatorBeforeNextAction(false);
    addAction(target, menu, BaseKit.collapseFoldAction);
    addAction(target, menu, BaseKit.expandFoldAction);
    setAddSeparatorBeforeNextAction(true);
    addAction(target, menu, BaseKit.collapseAllFoldsAction);
    addAction(target, menu, BaseKit.expandAllFoldsAction);
    // this is a hack, which assumes a certain action name from editor.fold.ui
    // if the action does not exist, nothing will be added to the menu.
    addAction(target, menu, "collapse-fold-tree"); // NOI18N
    addAction(target, menu, "expand-fold-tree"); // NOI18N
    // By default add separator before next actions (can be overriden if unwanted)
    setAddSeparatorBeforeNextAction(true);
    if (target != null) addAdditionalItems(target, menu);
    return menu;
}
 
Example 4
Source Project: netbeans   Source File: CslEditorKit.java    License: 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 5
Source Project: TrakEM2   Source File: LayerTree.java    License: GNU General Public License v3.0 6 votes vote down vote up
/** Get a custom, context-sensitive popup menu for the selected node. */
private JPopupMenu getPopupMenu(DefaultMutableTreeNode node) {
	Object ob = node.getUserObject();
	LayerThing thing = null;
	if (ob instanceof LayerThing) {
		thing = (LayerThing)ob;
	} else {
		return null;
	}
	// context-sensitive popup
	JMenuItem[] item = thing.getPopupItems(this);
	if (0 == item.length) return null;
	JPopupMenu popup = new JPopupMenu();
	for (int i=0; i<item.length; i++) {
		if (null == item[i] || "" == item[i].getText()) popup.addSeparator();
		else popup.add(item[i]);
	}
	return popup;
}
 
Example 6
@Override
public void actionPerformed (ActionEvent e)
{
    JMenuItem source = (JMenuItem) e.getSource();
    current = ShapeSet.valueOf(source.getText());

    if (current != null) {
        banner.setText(current.getName());

        Color color = current.getColor();

        if (color != null) {
            //colorChooser.setColor(color);
            refreshBanner();
        } else {
            banner.setForeground(Color.black);
        }

        paste.setEnabled(false);
        shapes.setRange();
    } else {
        banner.setText("");
    }
}
 
Example 7
Source Project: ontopia   Source File: VizPanel.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a menu item for hiding showing the neighbouring circle.
 */
protected void createDisEnableNeighCircMenuItem() {
  final String enableNeighbCirc = Messages
      .getString("Viz.EnableNeighbouringCircle");
  final String disableNeighbCirc = Messages
      .getString("Viz.DisableNeighbouringCircle");

  final JMenuItem disEnableNeighCircMenuItem = 
      new JMenuItem(controller.showNeighbouringCircle ?
          disableNeighbCirc : enableNeighbCirc);
  disEnableNeighCircMenuItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
      if (disEnableNeighCircMenuItem.getText().equals(enableNeighbCirc)) {
        disEnableNeighCircMenuItem.setText(disableNeighbCirc);
        controller.showNeighbouringCircle = true;
        controller.showNeighboursOnMouseover = true;
      } else {
        disEnableNeighCircMenuItem.setText(enableNeighbCirc);
        controller.showNeighbouringCircle = false;
        controller.showNeighboursOnMouseover = false;
      }
    }
  });
  add(glPopup, disEnableNeighCircMenuItem, ITEM_ID_ENABLE_NEIGH_CIRC);
}
 
Example 8
Source Project: WhiteRabbit   Source File: WhiteRabbitMain.java    License: Apache License 2.0 6 votes vote down vote up
private JMenuBar createMenuBar() {
	JMenuBar menuBar = new JMenuBar();
	JMenu helpMenu = new JMenu("Help");

	JMenuItem versionItem = new JMenuItem("White Rabbit v" +  Version.getVersion(this.getClass()));
	versionItem.setEnabled(false);
	helpMenu.add(versionItem);

	menuBar.add(helpMenu);
	JMenuItem helpItem = new JMenuItem(ACTION_CMD_HELP);
	helpItem.addActionListener(this);
	helpItem.setActionCommand(ACTION_CMD_HELP);
	helpMenu.add(helpItem);

	return menuBar;
}
 
Example 9
Source Project: megamek   Source File: MapMenu.java    License: GNU General Public License v2.0 6 votes vote down vote up
private JMenuItem createFlipArmsJMenuItem() {
    JMenuItem item = new JMenuItem("Flip Arms");

    item.setActionCommand(Integer.toString(myEntity.getId()));
    item.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                FiringDisplay display = (FiringDisplay) currentPanel;

                int id = Integer.parseInt(e.getActionCommand());
                display.updateFlipArms(!game.getEntity(id).getArmsFlipped());
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    });

    return item;
}
 
Example 10
protected void addPopupMenu( final JTable table )
{
	final JPopupMenu popupMenu = new JPopupMenu();
	final JMenuItem deleteItem = new JMenuItem( "Delete" );

	deleteItem.addActionListener(new ActionListener()
	{
		@Override
		public void actionPerformed( final ActionEvent e )
		{
			delete();
			System.out.println( "Right-click performed on table and choose DELETE" );
		}
	});

	popupMenu.add( deleteItem );

	table.setComponentPopupMenu( popupMenu );
}
 
Example 11
Source Project: netbeans   Source File: ButtonBuilders.java    License: Apache License 2.0 6 votes vote down vote up
static ComponentBuilder getBuilder(Instance instance, Heap heap) {
    if (DetailsUtils.isSubclassOf(instance, JButton.class.getName())) {
        return new JButtonBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JCheckBox.class.getName())) {
        return new JCheckBoxBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JRadioButton.class.getName())) {
        return new JRadioButtonBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JToggleButton.class.getName())) {
        return new JToggleButtonBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JCheckBoxMenuItem.class.getName())) {
        return new JCheckBoxMenuItemBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JRadioButtonMenuItem.class.getName())) {
        return new JRadioButtonMenuItemBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JMenu.class.getName())) {
        return new JMenuBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JMenuBar.class.getName())) {
        return new JMenuBarBuilder(instance, heap);
    } else if (DetailsUtils.isSubclassOf(instance, JMenuItem.class.getName())) {
        return new JMenuItemBuilder(instance, heap);
    }
    return null;
}
 
Example 12
Source Project: netbeans   Source File: ActionsTest.java    License: Apache License 2.0 6 votes vote down vote up
public void testCheckPrioritiesOfIcons() {
    AbstractAction aa = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    };
    Icon icon = ImageUtilities.loadImageIcon("org/openide/awt/TestIcon_big.png", true);
    aa.putValue(Action.SMALL_ICON, icon);
    aa.putValue("iconBase", "org/openide/awt/data/testIcon.gif");
    
    JButton b = new JButton();
    Actions.connect(b, aa);
    
    JMenuItem m = new JMenuItem();
    Actions.connect(m, aa, false);
    
    
    assertSame("Using the same icon (small" + icon, b.getIcon(), m.getIcon());
}
 
Example 13
Source Project: jdk8u-jdk   Source File: MenuItemIconTest.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void createUI() throws Exception {
    SwingUtilities.invokeAndWait(() -> {
        frame = new JFrame();
        frame.setTitle("Test");
        JMenuBar menuBar = new JMenuBar();
        ImageIcon icon = createIcon();
        menuItem = new JMenuItem("Command", icon);
        menuItem.setHorizontalTextPosition(SwingConstants.LEFT);
        menuBar.add(menuItem);
        frame.setJMenuBar(menuBar);
        frame.setPreferredSize(new Dimension(500, 500));
        frame.pack();
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
    });
}
 
Example 14
Source Project: arcusplatform   Source File: HubsMenu.java    License: Apache License 2.0 6 votes vote down vote up
private void rebuild(Store<HubModel> hubs, JMenu menu) {
   menu.removeAll();
   menu.add(registerHub);
   menu.add(registerHubLegacy);
   menu.add(reloadHub);
   menu.addSeparator();
   menu.add(decodeHubInfo);
   menu.addSeparator();

   if(hubs.size() == 0) {
      JMenuItem emptyMenu = new JMenuItem("Account has no hubs");
      emptyMenu.setEnabled(false);
      menu.add(emptyMenu);
   }
   else {
      for(HubModel hub: hubs.values()) {
         menu.add(new HubMenu(hub));
      }
   }
}
 
Example 15
Source Project: audiveris   Source File: BoardsPane.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Triggered when mouse is pressed.
 *
 * @param e mouse event
 */
@Override
public void mousePressed (MouseEvent e)
{
    if (isContextWanted(e)) {
        JPopupMenu popup = new SeparablePopupMenu();

        // A title for this menu
        JMenuItem head = new JMenuItem("Boards for selection:");
        head.setHorizontalAlignment(SwingConstants.CENTER);
        head.setEnabled(false);
        popup.add(head);
        popup.addSeparator();

        for (Board board : boards) {
            JMenuItem item = new JCheckBoxMenuItem(board.getName(), board.isSelected());
            item.addItemListener(this);
            item.setToolTipText(
                    board.isSelected() ? "Deselect this board?" : "Select this board?");
            popup.add(item);
        }

        popup.show(component, e.getX(), e.getY());
    }
}
 
Example 16
Source Project: chipster   Source File: MicroarrayMenuBar.java    License: MIT License 6 votes vote down vote up
private JMenuItem getLoadLocalSessionMenuItem(final boolean clear) {
	JMenuItem loadLocalSessionMenuItem = new JMenuItem();
	loadLocalSessionMenuItem.setText("Open local session...");
	if (clear) {
		loadLocalSessionMenuItem.setText("Open local session...");
		loadLocalSessionMenuItem.setAccelerator(KeyStroke.getKeyStroke('O', Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(), false));
	} else {
		loadLocalSessionMenuItem.setText("Local session...");
	}
	loadLocalSessionMenuItem.addActionListener(new java.awt.event.ActionListener() {
		public void actionPerformed(java.awt.event.ActionEvent e) {
			try {
				application.loadSession(false, false, clear);
			} catch (Exception ioe) {
				application.reportException(ioe);
			}
		}
	});
	return loadLocalSessionMenuItem;
}
 
Example 17
Source Project: netbeans   Source File: SetMainProject.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Get project weakly-referenced from an item.
 *
 * @param menuItem Menu item.
 *
 * @return The project, or null if it is not set or has been
 * garbage-collected.
 */
private static Project getItemProject(JMenuItem menuItem) {
    Reference<Project> p = (Reference<Project>) menuItem.getClientProperty(PROJECT_KEY);
    if (p == null) {
        return null;
    } else {
        return p.get();
    }
}
 
Example 18
Source Project: beautyeye   Source File: SwingSet2.java    License: Apache License 2.0 5 votes vote down vote up
/**
	 * Creates a JRadioButtonMenuItem for the Look and Feel menu.
	 *
	 * @param menu the menu
	 * @param label the label
	 * @param mnemonic the mnemonic
	 * @param accessibleDescription the accessible description
	 * @param laf the laf
	 * @return the j menu item
	 */
	public JMenuItem createLafMenuItem(JMenu menu, String label, String mnemonic,
			String accessibleDescription, String laf) {
		JMenuItem mi = (JRadioButtonMenuItem) menu.add(new JRadioButtonMenuItem(getString(label)));
		lafMenuGroup.add(mi);
		mi.setMnemonic(getMnemonic(mnemonic));
		mi.getAccessibleContext().setAccessibleDescription(getString(accessibleDescription));
//		mi.addActionListener(new ChangeLookAndFeelAction(this, laf));

//		mi.setEnabled(isAvailableLookAndFeel(laf));

		return mi;
	}
 
Example 19
Source Project: pcgen   Source File: PCGenMenuBar.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
private JMenu createHelpMenu()
{
	JMenu menu = new JMenu();
	menu.setText(LanguageBundle.getString("in_mnuHelp"));
	menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_DOCS_COMMAND)));
	menu.addSeparator();
	menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_OGL_COMMAND)));
	menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_TIPOFTHEDAY_COMMAND)));
	menu.addSeparator();
	menu.add(new JMenuItem(actionMap.get(PCGenActionMap.HELP_ABOUT_COMMAND)));
	return menu;
}
 
Example 20
Source Project: importer-exporter   Source File: GuiUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void setMnemonic(JMenuItem item, String labelKey, String indexKey) {		
	try {
		setMnemonic(item, Language.I18N.getString(labelKey), Integer.valueOf(Language.I18N.getString(indexKey)));
	} catch (NumberFormatException e) {
		//
	}
}
 
Example 21
Source Project: uima-uimaj   Source File: MainFrame.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Creates the recent text file item.
 *
 * @param num the num
 * @param file the file
 * @return the j menu item
 */
private final JMenuItem createRecentTextFileItem(int num, File file) {
  String fileShortName = file.getName();
  JMenuItem item = new JMenuItem(num + " " + fileShortName, getMnemonic(num));
  item.addActionListener(new LoadRecentTextFileEventHandler(this, file.getAbsolutePath()));
  item.setToolTipText(file.getAbsolutePath());
  return item;
}
 
Example 22
Source Project: beautyeye   Source File: SwingSet2.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Create a radio button menu menu item for items that are part of a
 * button group.
 *
 * @param menu the menu
 * @param label the label
 * @param mnemonic the mnemonic
 * @param accessibleDescription the accessible description
 * @param action the action
 * @param buttonGroup the button group
 * @return the j menu item
 */
private JMenuItem createButtonGroupMenuItem(JMenu menu, String label,
		String mnemonic,
		String accessibleDescription,
		Action action,
		ButtonGroup buttonGroup) {
	JRadioButtonMenuItem mi = (JRadioButtonMenuItem)menu.add(
			new JRadioButtonMenuItem(getString(label)));
	buttonGroup.add(mi);
	mi.setMnemonic(getMnemonic(mnemonic));
	mi.getAccessibleContext().setAccessibleDescription(getString(
			accessibleDescription));
	mi.addActionListener(action);
	return mi;
}
 
Example 23
Source Project: zap-extensions   Source File: ImportFromAbstractDialog.java    License: Apache License 2.0 5 votes vote down vote up
private static void setContextMenu(JTextField field) {
    JMenuItem paste =
            new JMenuItem(Constant.messages.getString(MESSAGE_PREFIX + "pasteaction"));
    paste.addActionListener(e -> field.paste());

    JPopupMenu jPopupMenu = new JPopupMenu();
    jPopupMenu.add(paste);
    field.setComponentPopupMenu(jPopupMenu);
}
 
Example 24
protected void prepareControls() {
    frame = new JFrame("Mixing : Dropdown Overlapping test");
    frame.setLayout(new GridLayout(0,1));
    frame.setSize(200, 200);
    frame.setVisible(true);

    menuBar = new JMenuBar();
    JMenu menu = new JMenu("Test Menu");
    ActionListener menuListener = new ActionListener() {

        public void actionPerformed(ActionEvent event) {
            lwClicked = true;
        }
    };

    JMenuItem item;
    menu.add(item = new JMenuItem("first"));
    item.addActionListener(menuListener);
    separator = new JSeparator();
    separator.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            spClicked = true;
        }
    });
    menu.add(separator);

    for (int i = 0; i < petStrings.length; i++) {
        menu.add(item = new JMenuItem(petStrings[i]));
        item.addActionListener(menuListener);
    }
    menuBar.add(menu);
    frame.setJMenuBar(menuBar);

    propagateAWTControls(frame);
    frame.setVisible(true);
}
 
Example 25
Source Project: netbeans   Source File: QueryBuilderInternalFrame.java    License: Apache License 2.0 5 votes vote down vote up
/** 
 * ActionListener interface implementation
 * This would presumably handle menu selections, but none of them actually
 * do anything now.
 */
public void actionPerformed(ActionEvent e) {
    if ( DEBUG ) {
        JMenuItem source = (JMenuItem)(e.getSource());
        String s = "Action event detected." // NOI18N
        + "\n" // NOI18N
        + "    Event source: " + source.getText() // NOI18N
        + " (an instance of " + getClassName(source) + ")"; // NOI18N
        System.out.println (s + "\n"); // NOI18N
    }
}
 
Example 26
Source Project: opensim-gui   Source File: LoggerTopComponent.java    License: Apache License 2.0 5 votes vote down vote up
private void addPopupCommands() {
    JMenuItem clearItem = jPopupMenu1.add("Clear");
    clearItem.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e) {
            jTextArea1.setText("");
        }});
}
 
Example 27
Source Project: openstego   Source File: OpenStegoFrame.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Getter method for helpAboutMenuItem
 *
 * @return helpAboutMenuItem
 */
public JMenuItem getHelpAboutMenuItem() {
    if (this.helpAboutMenuItem == null) {
        this.helpAboutMenuItem = new JMenuItem(labelUtil.getString("gui.menu.help.about"));
        this.helpAboutMenuItem.setMnemonic(KeyEvent.VK_A);
    }
    return this.helpAboutMenuItem;
}
 
Example 28
Source Project: WorldGrower   Source File: GuiMouseListener.java    License: GNU General Public License v3.0 5 votes vote down vote up
private JMenuItem createBestowCurseMenu(WorldObject target) {
	JMenuItem bestowCurseMenuItem = MenuFactory.createJMenuItem(new ChooseCurseAction(playerCharacter, imageInfoReader, soundIdReader, world, (WorldPanel)container, dungeonMaster, parentFrame, target), soundIdReader);
	bestowCurseMenuItem.setText(Actions.BESTOW_CURSE_ACTION.getSimpleDescription());
	bestowCurseMenuItem.setEnabled(Game.canActionExecute(playerCharacter, Actions.BESTOW_CURSE_ACTION, Args.EMPTY, world, target));
   	addToolTips(Actions.BESTOW_CURSE_ACTION, bestowCurseMenuItem);
   	setMenuIcon(bestowCurseMenuItem, Actions.BESTOW_CURSE_ACTION.getImageIds(playerCharacter));
	return bestowCurseMenuItem;
}
 
Example 29
Source Project: marathonv5   Source File: PopupMenuDemoX.java    License: Apache License 2.0 5 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    JMenuItem source = (JMenuItem) (e.getSource());
    String s = "Action event detected." + newline + "    Event source: " + source.getText() + " (an instance of "
            + getClassName(source) + ")";
    output.append(s + newline);
    output.setCaretPosition(output.getDocument().getLength());
}
 
Example 30
@Override
public JMenuItem getPopupPresenter() {
    List<PhpDocAction> docActions = new ArrayList<>(docProviders.size());
    for (PhpDocumentationProvider docProvider : docProviders) {
        docActions.add(new PhpDocAction(docProvider.getDisplayName(), phpModule, docProvider, true));
    }
    return new DocumentationSubMenu(docActions);
}