4

How to display a popup menu by mouse left click? I know the default is for mouse right click. But I want to expand(display) the menu just by a normal selection of a button. (by normal left click). How to popup a popup menu by normal right click is as follows.

final Button btnNewgroup = new Button(compositeTextClient, SWT.NONE);
Menu menu = new Menu(btnNewgroup);
btnNewgroup.setMenu(menu);
MenuItem mntmNewItem = new MenuItem(menu, SWT.NONE);
mntmNewItem.setText("New Item");
MenuItem mntmNewItem2 = new MenuItem(menu, SWT.NONE);
mntmNewItem2.setText("New Item2");
Baz
  • 36,440
  • 11
  • 68
  • 94
Samitha Chathuranga
  • 1,689
  • 5
  • 30
  • 57

1 Answers1

8

Use a selection listener on the button:

btnNewgroup.addSelectionListener(new SelectionAdapter() {
  @Override
  public void widgetSelected(final SelectionEvent e)
  {
    Rectangle bounds = btnNewgroup.getBounds();

    Point point = btnNewgroup.getParent().toDisplay(bounds.x, bounds.y + bounds.height);

    menu.setLocation(point);

    menu.setVisible(true);
  }
});
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 1
    thanks. It worked. Actually menu.setVisible(true); is just enough to include in the widgetSelected() method. But is there any way to remove the default right click action of the menu? Because now the menu is poped for both right and left clicks. – Samitha Chathuranga Sep 30 '14 at 04:49