0

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

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by FALL3N
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.