1

How to add right-mouse click listener on a TableViewer item?

Baz
  • 36,440
  • 11
  • 68
  • 94

2 Answers2

5

If you are referring to a JFace TableViewer, you can use the following approach assuming viewer is your TableViewer.

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

manager.add(new Action("MENU ITEM TEXT", ImageDescriptor.createFromImage(YOUR_IMAGE)) {
    @Override
    public void run() {
        // get the current selection of the tableviewer
        IStructuredSelection selection = (IStructuredSelection) viewer.getSelection();
        // do something
    }
});

Otherwise, please clarify your question.

Baz
  • 36,440
  • 11
  • 68
  • 94
  • 1
    how to add RIGHT-mouse click listener? – Sergej Paskevic Oct 12 '12 at 08:02
  • @SergejPaskevic This is a right-click menu. Do you want a menu or just listen to the right click event? – Baz Oct 12 '12 at 08:04
  • @Baz just wanted to listen to the right click event, I am new to Eclipse Plugin, so this might be silly question but is there anything other then `TableViwer` by which right click could be listen. – Vishrant May 08 '14 at 09:18
  • @Vishrant To which widget do you want to add the right-click feature? – Baz May 08 '14 at 09:21
  • I just wanted to create a plugin that will display some `"Mouse Right Button Click"` message on mouse right click. – Vishrant May 08 '14 at 09:24
  • @Vishrant Yeah, I understand that, but **where** do you want the right mouse click to happen? Everywhere? Just on some widgets? – Baz May 08 '14 at 09:30
  • 1
    @Vishrant Then use [this](http://stackoverflow.com/a/18403738/1740724) answer and add the `Menu` to every `Control` you want it to show on using [`Control#setMenu()`](http://help.eclipse.org/helios/topic/org.eclipse.platform.doc.isv/reference/api/org/eclipse/swt/widgets/Control.html#setMenu(org.eclipse.swt.widgets.Menu)) – Baz May 08 '14 at 10:10
2

You can get the Table of the TableViewer and call

public void addMouseListener (MouseListener listener)

the MouseEvent will tell you which mouse-button was pressed

/**
 * the button that was pressed or released; 1 for the
 * first button, 2 for the second button, and 3 for the
 * third button, etc.
 */
public int button;
Baz
  • 36,440
  • 11
  • 68
  • 94
Markus Lausberg
  • 12,177
  • 6
  • 40
  • 66