Hiding menu items in Jface MenuManager and TreViewer

陌路散爱 提交于 2019-12-11 07:58:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!