Java Utililty Methods JMenuItem

List of utility methods to do JMenuItem

Description

The list of methods to do JMenuItem are organized into topic(s).

Method

JMenuItemcreateJMenuItem(JMenu menu, String text, int mnemonic, int acceleratorKey, String toolTip, ActionListener actionListener)
create J Menu Item
JMenuItem menuItem = new JMenuItem();
menuItem.setText(text);
if (mnemonic > 0)
    menuItem.setMnemonic(mnemonic);
if (acceleratorKey > 0)
    menuItem.setAccelerator(KeyStroke.getKeyStroke(acceleratorKey, ActionEvent.CTRL_MASK));
if (toolTip != null)
    menuItem.setToolTipText(toolTip);
...
JMenucreateMenu(JMenuItem[] items, String name, String description, int mnemonic)
Makes a new top-level menu.
JMenu menu = new JMenu(name);
menu.getAccessibleContext().setAccessibleDescription(description);
menu.setMnemonic(mnemonic);
addAll(items, menu);
return menu;
voidexit(JMenuItem anItem)
exit
anItem.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent event) {
        System.exit(0);
});
ColorgetForeground(final BasicMenuItemUI ui, final JMenuItem menuItem)
get Foreground
final SynthContext context = getSynthContext(ui, menuItem);
return context.getStyle().getColor(context, ColorType.TEXT_FOREGROUND);
intgetIconPlaceholderWidth(final JMenuItem menuItem, final boolean alignTextToMenuIcons)
Returns maximum icon width for this menu item.
if (alignTextToMenuIcons && menuItem.getParent() instanceof JPopupMenu) {
    int max = 0;
    final JPopupMenu popupMenu = (JPopupMenu) menuItem.getParent();
    for (int i = 0; i < popupMenu.getComponentCount(); i++) {
        final Component component = popupMenu.getComponent(i);
        if (component instanceof JMenuItem) {
            final JMenuItem otherItem = (JMenuItem) component;
            if (otherItem.getIcon() != null) {
...
ComponentgetInvoker(final JMenuItem menuItem)
get Invoker
MenuContainer menuContainer = menuItem.getParent();
while (menuContainer != null && !(menuContainer instanceof JPopupMenu)) {
    if (menuContainer instanceof MenuItem) {
        menuContainer = ((MenuItem) menuContainer).getParent();
    } else {
        menuContainer = null;
if (menuContainer != null) {
    final JPopupMenu menu = (JPopupMenu) menuContainer;
    final Component invoker = menu.getInvoker();
    return invoker;
} else {
    return null;
JMenuItemgetJMenuItem(String name, JMenuBar jmenubar)
get J Menu Item
JMenuItem returnedItem = null;
JMenuBar jbar = jmenubar;
for (int i = 0; i < jbar.getMenuCount(); i++) {
    JMenu jmenu = jbar.getMenu(i);
    for (int j = 0; j < jmenu.getItemCount(); j++) {
        JMenuItem jitem = jmenu.getItem(j);
        if (name.equals(jitem.getText())) {
            returnedItem = jitem;
...
JComponentgetMenuItemParent(JMenuItem menuItem)
Returns parent of this component if it is not a top-level menu Otherwise returns null.
Container parent = menuItem.getParent();
if ((parent instanceof JComponent)
        && (!(menuItem instanceof JMenu) || !((JMenu) menuItem).isTopLevelMenu())) {
    return (JComponent) parent;
} else {
    return null;
JMenugetTopLevelMenu(JMenuItem menuitem)
Determines the top-level menu (the one sitting inside the JMenuBar) that is associated with a menu item.
JMenu result;
Container cont;
Container prev;
result = null;
cont = menuitem;
prev = null;
while (cont != null) {
    if (cont instanceof JPopupMenu) {
...
StringlayoutMenuItem(JMenuItem menuItem, FontMetrics fm, String text, FontMetrics fmAccel, String acceleratorText, Icon icon, Icon checkIcon, Icon arrowIcon, int verticalAlignment, int horizontalAlignment, int verticalTextPosition, int horizontalTextPosition, Rectangle viewRect, Rectangle iconRect, Rectangle textRect, Rectangle acceleratorRect, Rectangle checkIconRect, Rectangle arrowIconRect, int textIconGap, int menuItemGap)
Compute and return the location of the icons origin, the location of origin of the text baseline, and a possibly clipped version of the compound labels string.
SwingUtilities.layoutCompoundLabel(menuItem, fm, text, icon, verticalAlignment, horizontalAlignment,
        verticalTextPosition, horizontalTextPosition, viewRect, iconRect, textRect, textIconGap);
if ((acceleratorText == null) || acceleratorText.equals("")) {
    acceleratorRect.width = acceleratorRect.height = 0;
    acceleratorText = "";
} else {
    acceleratorRect.width = fmAccel.stringWidth(acceleratorText);
    acceleratorRect.height = fmAccel.getHeight();
...