How to display a popup menu by mouse left click in

2019-05-07 18:07发布

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");

1条回答
时光不老,我们不散
2楼-- · 2019-05-07 18:44

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);
  }
});
查看更多
登录 后发表回答