MenuItem Arm Event Listener : MenuItem Event « SWT « Java Tutorial






MenuItem Arm Event 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 ArmEventListenerMenuItem {

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

    Listener armListener = new Listener() {
      public void handleEvent(Event event) {
        MenuItem item = (MenuItem) event.widget;
        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);
    fileItem.setMenu(fileMenu);

    String[] fileStrings = { "New", "Close", "Exit" };
    for (int i = 0; i < fileStrings.length; i++) {
      MenuItem item = new MenuItem(fileMenu, SWT.PUSH);
      item.setText(fileStrings[i]);
      item.addListener(SWT.Arm, armListener);
    }    
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }

}








17.33.MenuItem Event
17.33.1.Add Selection Listener to MenuItemAdd Selection Listener to MenuItem
17.33.2.Add click event listener to MenuItem
17.33.3.MenuItem Arm Event ListenerMenuItem Arm Event Listener