Java JMenuItem replaceMenuItem( @Nonnull JMenuItem orginalMenuItem, @Nonnull JMenuItem replacementMenuItem)

Here you can find the source of replaceMenuItem( @Nonnull JMenuItem orginalMenuItem, @Nonnull JMenuItem replacementMenuItem)

Description

replace a menu item in its parent with another menu item

(at the same position in the parent menu)

License

Open Source License

Parameter

Parameter Description
orginalMenuItem the original menu item to be replaced
replacementMenuItem the menu item to replace it with

Return

true if the original menu item was found and replaced

Declaration

public static boolean replaceMenuItem(
        @Nonnull JMenuItem orginalMenuItem,
        @Nonnull JMenuItem replacementMenuItem) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.Container;

import javax.annotation.Nonnull;

import javax.swing.JMenuItem;

public class Main {
    /**/* ww  w  .j  a va  2  s  . c o m*/
     * replace a menu item in its parent with another menu item
     * <p>
     * (at the same position in the parent menu)
     *
     * @param orginalMenuItem     the original menu item to be replaced
     * @param replacementMenuItem the menu item to replace it with
     * @return true if the original menu item was found and replaced
     */
    public static boolean replaceMenuItem(
            @Nonnull JMenuItem orginalMenuItem,
            @Nonnull JMenuItem replacementMenuItem) {
        boolean result = false; // assume failure (pessimist!)
        Container container = orginalMenuItem.getParent();
        if (container != null) {
            int index = container.getComponentZOrder(orginalMenuItem);
            if (index > -1) {
                container.remove(orginalMenuItem);
                container.add(replacementMenuItem, index);
                result = true;
            }
        }
        return result;
    }
}

Related

  1. getTopLevelMenu(JMenuItem menuitem)
  2. layoutMenuItem(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)
  3. linkMenuItem(final JMenuItem master, final JMenuItem slave)
  4. pintaBarraMenu(Graphics g, JMenuItem menuItem, Color bgColor)
  5. recursiveUpdateOrInsertMenuItem(MenuElement menu, JMenuItem menuItem, boolean hideOnUpdate)
  6. setAccelerator(JMenuItem menuItem, int key, int mask)
  7. setCtrlAccelerator(final JMenuItem jmi, final char accelerator)
  8. setCtrlAccelerator(JMenuItem jmi, char accelerator)
  9. setEnabled(boolean enabled, JMenuItem... items)