문제

I am trying to add an action to a menu to the header (titles) of a TableViewer.

This is the code that I am using now:

    viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL 
                                         | SWT.FULL_SELECTION | SWT.BORDER);
    //...

    MenuManager manager = new MenuManager();
    viewer.getControl().setMenu(manager.createContextMenu(viewer.getControl()));


    manager.add(new Action("MENU ITEM TEXT") {
        @Override
        public void run() {
            // get the current selection of the tableviewer
            IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
            // do something
            if (selection.getFirstElement() instanceof MyObject)
                return;
            System.out.println("OK: "+selection.getFirstElement().getClass().getName());
        }
    });

And this is how it looks:

enter image description here

The problem is that the menu gets added to the entire TableViewer, not only to the header row. Because for the other rows I will need to use a different menu. I have tryied to find a way of adding the action only to the top row (the titles row), but with no success so far.

So how can I add the menu only for the header?

도움이 되었습니까?

해결책

You add a MouseListener to the table, and you check for the following things:

  1. Clicked button is mouse right-click.
  2. The pointer of the event is located within the bounds of your table item (i.e. your first TableItem - you will use table.getItem(Point)).

If these conditions are met, you open the menu at mouse location.

다른 팁

Actually, here's a snippet of how this can be done.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top