Showing a right click menu for a SWT TableItem?
Asked Answered
A

3

5

Is it possible to show a right click menu on table items with SWT? The menu would be different for every item, e.g 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?

Arciniega answered 23/9, 2013 at 18:22 Comment(4)
possible duplicate of Adding right click menu to to treeitem in SWT treeInterclavicle
Link above works analogously for a Table instead of a Tree.Interclavicle
@Interclavicle That's not really the same as what I asked here, such as being specific to the row being right clicked.Arciniega
How so? Within the Listener, you know which row was clicked on. Just create/enable/disable the MenuItems accordingly.Interclavicle
A
1
    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

Arciniega answered 23/9, 2013 at 19:9 Comment(5)
What's a DynamicTable?Interclavicle
@Interclavicle I think it could be: code.google.com/a/eclipselabs.org/p/opal/wiki/…Motherly
@ClickUpvote I see, so it's completely unrelated to the question?Interclavicle
If he's using the same DynamicTable, that one works the same like the regular SWT TableMotherly
@ClickUpvote I didn't mean that the answer is unrelated, just the usage of a different table class (although almost identical).Interclavicle
H
6

Listening for SWT.MouseDown, as suggested by @user4793956, is completely useless. The context menu is always brought up, no need to call setVisible(true). Quite contrary, you need to cancel the SWT.MenuDetect event, if you do not want the menu to pop up.

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;
    }
  }
});
Hui answered 3/11, 2015 at 13:49 Comment(1)
The problem of this code is, menu item "Test Item" will come for table header also.Rocher
D
2

Without using a 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);
            }

        }

    });
Dolf answered 15/4, 2015 at 21:30 Comment(2)
The code won't work, unless you declare contextMenu as final.Hui
@Hui unless you use Java 8Everyday
A
1
    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

Arciniega answered 23/9, 2013 at 19:9 Comment(5)
What's a DynamicTable?Interclavicle
@Interclavicle I think it could be: code.google.com/a/eclipselabs.org/p/opal/wiki/…Motherly
@ClickUpvote I see, so it's completely unrelated to the question?Interclavicle
If he's using the same DynamicTable, that one works the same like the regular SWT TableMotherly
@ClickUpvote I didn't mean that the answer is unrelated, just the usage of a different table class (although almost identical).Interclavicle

© 2022 - 2024 — McMap. All rights reserved.