简体   繁体   English

显示SWT TableItem的右键菜单?

[英]Showing a right click menu for a SWT TableItem?

Is it possible to show a right click menu on table items with SWT? 是否可以使用SWT在表项目上显示右键菜单? The menu would be different for every item, eg for some rows, some of the menu items would be enabled, for others, they would be disabled. 菜单对于每个项目都是不同的,例如对于某些行,某些菜单项将被启用,而对于其他项,它们将被禁用。 So, each row would need its own menu, and when setting up the menu i'd need a way to identify which row I was working with. 因此,每一行都需要自己的菜单,在设置菜单时我需要一种方法来识别我正在使用哪一行。

Any ideas? 有任何想法吗?

Listening for SWT.MouseDown , as suggested by @user4793956, is completely useless. 听力SWT.MouseDown ,由@ user4793956建议,是完全无用的。 The context menu is always brought up, no need to call setVisible(true) . 总是启动上下文菜单,无需调用setVisible(true) Quite contrary, you need to cancel the SWT.MenuDetect event, if you do not want the menu to pop up. 完全相反,你需要取消SWT.MenuDetect情况下,如果你希望菜单弹出。

This works for me: 这对我有用:

// Create context menu
Menu menuTable = new Menu(table);
table.setMenu(menuTable);

// Create menu item
MenuItem miTest = new MenuItem(menuTable, SWT.NONE);
miTest.setText("Test Item");

// Do not show menu, when no item is selected
table.addListener(SWT.MenuDetect, new Listener() {
  @Override
  public void handleEvent(Event event) {
    if (table.getSelectionCount() <= 0) {
      event.doit = false;
    }
  }
});

Without using a DynamicTable: 不使用DynamicTable:

    Menu contextMenu = new Menu(table);
    table.setMenu(contextMenu);
    MenuItem mItem1 = new MenuItem(contextMenu, SWT.None);
    mItem1.setText("Menu Item Test.");

    table.addListener(SWT.MouseDown, new Listener(){

        @Override
        public void handleEvent(Event event) {
            TableItem[] selection = table.getSelection();
            if(selection.length!=0 && (event.button == 3)){
                contextMenu.setVisible(true);
            }

        }

    });
    table = new DynamicTable(shell, SWT.BORDER | SWT.FULL_SELECTION);        
    table.addMenuDetectListener(new MenuDetectListener()
    {
        @Override
        public void menuDetected(MenuDetectEvent e)
        {                
           int index = table.getSelectionIndex();
           if (index == -1) 
             return; //no row selected

           TableItem item = table.getItem(index);
           item.getData(); //use this to identify which row was clicked.
           //The popup can now be displayed as usual using table.toDisplay(e.x, e.y)              
        }
    });

More details: http://www.eclipsezone.com/eclipse/forums/t49734.html 更多细节: http//www.eclipsezone.com/eclipse/forums/t49734.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM