问题
I want to hide all popup menu items when I click on any node that is not "Item1", but the items are not being hidden, why?
(The manager.getItems()[i].setVisible(false)
inside loop is triggered)
Action a1 = new Action("XXX") {};
Action a2 = new Action("YYY") {};
Action a3 = new Action("ZZZ") {};
final MenuManager mgr = new MenuManager();
mgr.add(a1);
mgr.add(a2);
mgr.add(a3);
mgr.addMenuListener(new IMenuListener() {
public void menuAboutToShow(IMenuManager manager) {
IStructuredSelection selection = (IStructuredSelection) tree
.getSelection();
if (!selection.isEmpty()) {
String str = ((MyModel) selection.getFirstElement())
.toString();
if (str.equals("Item1")) {
for (int i = 0; i < 3; i++) {
manager.getItems()[i].setVisible(true);
}
} else {
for (int i = 0; i < 3; i++) {
manager.getItems()[i].setVisible(false);
}
}
}
}
});
When first I click on Item (str.equals("Item1")
), clicking on other items don't hide the menu items.
回答1:
You have to call manager.update(true);
after setting the visibility of your menu items to make the MenuManager
update the underlying Menu
widget.
回答2:
Yes, you can do something like
for(val menuItem : menu.getItems()){
menuItem.setVisible(yourCondition);
}
menu.update(true);
I've tested it.
来源:https://stackoverflow.com/questions/7825162/hiding-menu-items-in-jface-menumanager-and-treviewer