下面列出了javax.swing.JMenuBar#remove ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* On Mac often the menus won't really update without this hack-ish twist:
* remove the menu and re-add it. Voila! It's both unnecessary and crucial
* at the same time.
*
* @param f
* the frame whose menubar you need to update.
* @param menu
* the menu you need to update.
*/
static void fixMenuBar(JFrame f, JMenu menu) {
JMenuBar mb = f.getJMenuBar();
if (mb != null) {
JMenu[] menus = new JMenu[mb.getMenuCount()];
for (int a = 0; a < menus.length; a++) {
menus[a] = mb.getMenu(a);
}
boolean found = false;
List<JMenu> menusToAdd = new ArrayList<>();
for (int a = 0; a < menus.length; a++) {
if (menus[a] == menu)
found = true;
if (found) {
mb.remove(menus[a]);
menusToAdd.add(menus[a]);
}
}
for (JMenu menuToAdd : menusToAdd) {
mb.add(menuToAdd);
}
}
}
/**
* Removes a menu with the given name from the menu bar and returns the removed item.
* Returns null if menu item does not exist.
*
* @param menuName String
* @return JMenu
*/
public JMenu removeMenu(String menuName) {
JMenuBar menuBar = getJMenuBar();
if(menuBar==null) {
return null;
}
menuName = menuName.trim();
JMenu menu = null;
for(int i = 0; i<menuBar.getMenuCount(); i++) {
JMenu next = menuBar.getMenu(i);
if(next.getText().trim().equals(menuName)) {
menu = next;
menuBar.remove(i);
break;
}
}
return menu;
}
/**
* Remove the menu from BURP menu bar.
*
* @see "https://github.com/PortSwigger/param-miner/blob/master/src/burp/Utilities.java"
*/
@Override
public void extensionUnloaded() {
JFrame burpFrame = ConfigMenu.getBurpFrame();
if (burpFrame != null && this.cfgMenu != null) {
JMenuBar jMenuBar = burpFrame.getJMenuBar();
jMenuBar.remove(this.cfgMenu);
jMenuBar.repaint();
this.trace.writeLog("Configuration menu removed.");
} else {
this.trace.writeLog("Cannot remove the configuration menu (ref on the BURP frame is null).");
}
}
@Override
public void finishMenu(String path) {
path = mapping(path);
if (path.equals("_") || path.startsWith("_/")) {
return;
}
if (!menuElements.containsKey(path)) {
throw new IllegalArgumentException("Invalid menu: " + path);
}
if (path.startsWith("/file/recent")) {
return;
}
MenuElement me = menuElements.get(path);
if (me instanceof JMenu) {
JMenu jm = (JMenu) me;
if (jm.getMenuComponentCount() == 1) {
String parentPath = path.contains("/") ? path.substring(0, path.lastIndexOf('/')) : "";
MenuElement parMe = menuElements.get(parentPath);
JMenuItem mi = (JMenuItem) jm.getMenuComponent(0);
jm.remove(mi);
if (parMe instanceof JMenu) {
JMenu parMenu = (JMenu) parMe;
parMenu.remove(jm);
parMenu.add(mi);
} else if (parMe instanceof JMenuBar) {
JMenuBar parMenuBar = (JMenuBar) parMe;
parMenuBar.remove(jm);
parMenuBar.add(mi);
}
}
}
}