Menu Show Action Listener : MenuEvent « SWT « Java Tutorial






Menu Show Action Listener
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;

public class MenuActionListenerShow {

  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    

    Listener showListener = new Listener() {
      public void handleEvent(Event event) {
        Menu menu = (Menu) event.widget;
        MenuItem item = menu.getParentItem();
        if (item != null) {
          System.out.println(item.getText());
        
        }
      }
    };

    Menu menuBar = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menuBar);
    MenuItem fileItem = new MenuItem(menuBar, SWT.CASCADE);
    fileItem.setText("File");
    MenuItem editItem = new MenuItem(menuBar, SWT.CASCADE);
    editItem.setText("Edit");
    Menu fileMenu = new Menu(shell, SWT.DROP_DOWN);
    fileMenu.addListener(SWT.Show, showListener);
    fileItem.setMenu(fileMenu);

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    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