Menu Examples : Menu « SWT JFace Eclipse « Java

Menu Examples

Menu Examples
 * All Right Reserved. Copyright (c) 1998, 2004 Jackwind Li Guojie
 * Created on Feb 24, 2004 2:23:13 PM by JACK $Id$

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 MenuExamples {
  Display display = new Display();
  Shell shell = new Shell(display);

  public MenuExamples() {

    Menu menuBar = new Menu(shell, SWT.BAR);
    MenuItem itemHello = new MenuItem(menuBar, SWT.PUSH);
    itemHello.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {

    MenuItem itemCascade = new MenuItem(menuBar, SWT.CASCADE);
    itemCascade.setText("&CASCADE item");

    Menu menu = new Menu(itemCascade);

    MenuItem itemPush = new MenuItem(menu, SWT.PUSH);
    itemPush.setText("&PUSH item\tCtrl+P");
    itemPush.setAccelerator(SWT.CTRL + 'P');
    Image icon = new Image(shell.getDisplay(), "java2s.gif");
    itemPush.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        System.out.println("item selected: PUSH item");

    final MenuItem itemCheck = new MenuItem(menu, SWT.CHECK);
    itemCheck.setText("CHEC&K item\tCtrl+K");
    itemCheck.setAccelerator(SWT.CTRL + 'K');
    itemCheck.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        System.out.println("item selected: CHECK item");
        System.out.println("Selection: " + itemCheck.getSelection());
    new MenuItem(menu, SWT.SEPARATOR);

    final MenuItem itemRadio = new MenuItem(menu, SWT.RADIO);
    itemRadio.setText("&RADIO item\tCtrl+R");
    itemRadio.setAccelerator(SWT.CTRL + 'R');
    itemRadio.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        System.out.println("item selected: RADIO item");
        System.out.println("Selection: " + itemRadio.getSelection());



    shell.setSize(300, 120);;

    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue


  private void init() {


  public static void main(String[] args) {
    new MenuExamples();


Related examples in the same category

1.SWT Menu and menu eventSWT Menu and menu event
2.Shared Menu in SWTShared Menu in SWT
3.Demonstrates menusDemonstrates menus
4.Menu Shell
5.Menu Shell 2Menu Shell 2
6.Menu Shell 3Menu Shell 3
7.Menu Shell 4Menu Shell 4
8.Menu Shell 6Menu Shell 6
9.Menu Shell 5Menu Shell 5
10.SWT Menu ExampleSWT Menu Example
11.Show a popup menu (wait for it to close)Show a popup menu (wait for it to close)
12.Fill a menu dynamically (when menu shown)Fill a menu dynamically (when menu shown)
13.Enable menu items dynamically (when menu shown)Enable menu items dynamically (when menu shown)
14.Create a menu with radio itemsCreate a menu with radio items
15.Create a popup menu (set in multiple controls)Create a popup menu (set in multiple controls)
16.Create a bar and pull down menu (accelerators, mnemonics)Create a bar and pull down menu (accelerators, mnemonics)