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();
}
}
|