Add Menu event to popup menu : MenuEvent « SWT « Java Tutorial






Add Menu event to popup menu
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.Clipboard;
import org.eclipse.swt.events.MenuAdapter;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class PopupMenuMenuEvent {

  public static void main(String[] args) {
    Display display = new Display();
    final Clipboard cb = new Clipboard(display);
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Text text = new Text(shell, SWT.BORDER | SWT.MULTI | SWT.WRAP);
    Menu menu = new Menu(shell, SWT.POP_UP);
    final MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
    
    copyItem.setText("Copy");
    
    menu.addMenuListener(new MenuAdapter() {
      public void menuShown(MenuEvent e) {
        System.out.println("Menu event");
      }
    });    
    
    text.setMenu(menu);

    shell.setSize(200, 200);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    cb.dispose();
    display.dispose();
  }
}








17.31.MenuEvent
17.31.1.Add Menu event to popup menuAdd Menu event to popup menu
17.31.2.Menu Show Action ListenerMenu Show Action Listener