Can anyone figure out why the following code is not showing the menubar when it is run in eclipse.

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;

public class MenuExample {
    Display display;
    Shell shell;
    Menu menu, fileMenu, editMenu,viewMenu;

public MenuExample() {
    display = new Display();
    shell = new Shell(display);
    shell.setText("Menu Example");
    shell.setSize(300, 400);

    menu = new Menu(shell, SWT.BAR);
    shell.setMenuBar(menu);

    MenuItem fileItem = new MenuItem(menu, SWT.CASCADE);
    fileItem.setText("File");
    MenuItem editItem = new MenuItem(menu, SWT.CASCADE);
    editItem.setText("Edit");
    MenuItem viewItem = new MenuItem(menu, SWT.CASCADE);
    viewItem.setText("View");
    MenuItem helpItem = new MenuItem(menu, SWT.CASCADE);
    helpItem.setText("Help");

    Menu fileMenu = new Menu(menu);
    fileItem.setMenu(fileMenu);
    MenuItem newItem = new MenuItem(fileMenu, SWT.NONE);
    newItem.setText("New");
    MenuItem openItem = new MenuItem(fileMenu, SWT.NONE);
    openItem.setText("Open...");
    MenuItem saveItem = new MenuItem(fileMenu, SWT.NONE);
    saveItem.setText("Save");
    MenuItem saveAsItem = new MenuItem(fileMenu, SWT.NONE);
    saveAsItem.setText("Save As...");
    new MenuItem(fileMenu, SWT.SEPARATOR);
    MenuItem pageSetupItem = new MenuItem(fileMenu, SWT.NONE);
    pageSetupItem.setText("Page Setup...");
    MenuItem printItem = new MenuItem(fileMenu, SWT.NONE);
    printItem.setText("Print...");
    new MenuItem(fileMenu, SWT.SEPARATOR);
    MenuItem exitItem = new MenuItem(fileMenu, SWT.NONE);
    exitItem.setText("Exit");

    Menu editMenu = new Menu(menu);
    editItem.setMenu(editMenu);
    MenuItem cutItem = new MenuItem(editMenu, SWT.NONE);
    cutItem.setText("Cut");
    MenuItem pasteItem = new MenuItem(editMenu, SWT.NONE);
    pasteItem.setText("Paste");

    Menu viewMenu = new Menu(menu);
    viewItem.setMenu(viewMenu);
    MenuItem toolItem = new MenuItem(viewMenu, SWT.NONE);
    toolItem.setText("ToolBars");
    MenuItem fontItem = new MenuItem(viewMenu, SWT.NONE);
    fontItem.setText("Font");

    shell.open();
    shell.setVisible(true);

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
}




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

yeah, u are adding the menus to another menu.. u gotta have menus in a Menu Bar which can then be displayed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.