Here you can find the source of replaceMenuItem( @Nonnull JMenuItem orginalMenuItem, @Nonnull JMenuItem replacementMenuItem)
(at the same position in the parent menu)
Parameter | Description |
---|---|
orginalMenuItem | the original menu item to be replaced |
replacementMenuItem | the menu item to replace it with |
public static boolean replaceMenuItem( @Nonnull JMenuItem orginalMenuItem, @Nonnull JMenuItem replacementMenuItem)
//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; } }