【问题标题】:Add Context Menu to a dialogue in e4 rcp将上下文菜单添加到 e4 rcp 中的对话
【发布时间】:2015-01-27 04:08:55
【问题描述】:

我想在对话中添加上下文菜单。我希望它以这样一种方式单击,当单击它为空的任何地方时,会出现一个默认的上下文菜单。我已经看到将上下文菜单添加到表格和树中的示例,但不是作为一个整体的对话,任何 sn-ps 或示例将不胜感激。

这是我尝试过的。

import org.eclipse.jface.action.MenuManager;

@Override
protected Control createDialogArea(Composite parent) {

    Composite container = (Composite) super.createDialogArea(parent);
    GridLayout layout = new GridLayout(4, false);
    layout.marginRight = 5;
    layout.marginLeft = 10;
    container.setLayout(layout);

    MenuManager menuMgr = new MenuManager();
    menuMgr.setRemoveAllWhenShown(true);
    menuMgr.add(new Action("New Thing") {

        /* (non-Javadoc)
         * @see org.eclipse.jface.action.Action#run()
         */
        @Override
        public void run() {
            System.out.println("came in options");
        }
    });

    parent.setMenu(menuMgr.createContextMenu(parent));
    productListTreeCheckBox(parent);
    return super.createDialogArea(parent);
}

【问题讨论】:

    标签: rcp e4


    【解决方案1】:

    尝试以与表格/树形菜单类似的方式创建菜单,但在对话框中使用顶级Composite。使用可能类似于以下内容的菜单管理器:

    Control topLevelComposite = ... get top level composite
    
    MenuManager menuMgr = new MenuManager();
    menuMgr.setRemoveAllWhenShown(true);
    
    menuMgr.addMenuListener(... you menu listener....);
    
    final Menu menu = menuMgr.createContextMenu(topLevelComposite);
    
    topLevelComposite.setMenu(menu);
    

    然后您必须在每个Composite 上调用setMenu 并在对话框中控制您要使用此菜单的位置。您可以使用:

    control.setMenu(parent.getMenu());
    

    为此(只要您从topLevelComposite 的孩子开始的所有事情上都这样做)。

    【讨论】:

    • 要么我做错了,要么我触摸了错误的组件。我将输入我的代码 sn-p 这可能会说明我做错了什么。
    • 用容器交换父级也不起作用。类似的 sn-ps 用于在表中显示上下文菜单。
    • 您必须在对话框中创建的每个 Composite 和 Control 上调用 setMenu。
    • parent.setMenu(menuMgr.createContextMenu(parent));这不是你说的setmenu的意思吗?
    • 您在顶层复合材料上使用它,而对所有孩子使用另一个调用
    猜你喜欢
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多