burgerLove
burgerLove

Reputation: 35

Java SWT exception : "java.lang.IllegalArgumentException" : Menu is not a POP_UP

I try to create a Menu using the SWT object "Menu". Lines of codes are rather simple :

public static void main(String[] args) {

    createShell();
}


private static void createShell() {

    Display display = new Display();
    Shell shell = new Shell(display, SWT.SHELL_TRIM);

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

    shell.open();

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

    display.dispose();

}

At runtime, I get the following exception :

Exception in thread "main" java.lang.IllegalArgumentException: Menu is not a POP_UP
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.SWT.error(Unknown Source)
at org.eclipse.swt.widgets.Widget.error(Unknown Source)
at org.eclipse.swt.widgets.Control.setMenu(Unknown Source)
at labo.Laboratory.createShell(Laboratory.java:25)
at labo.Laboratory.main(Laboratory.java:15)

the swt code that seems to to be involved is the one from the "setMenu" method :

public void setMenu (Menu menu) {
checkWidget ();
if (menu != null) {
    if (menu.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
    if ((menu.style & SWT.POP_UP) == 0) {
        error (SWT.ERROR_MENU_NOT_POP_UP);
    }
    if (menu.parent != menuShell ()) {
        error (SWT.ERROR_INVALID_PARENT);
    }
}
this.menu = menu;

}

Obvisously I go to the following line of code :

if ((menu.style & SWT.POP_UP) == 0) {
    error (SWT.ERROR_MENU_NOT_POP_UP);
}

But I cannot understand why. The code I try to execute is given as a valid example everywhere, but it does not work for me.

I am using eclipse IDE, with a Java 8 JRE. SWT library is the one provided by eclipse neon. The OS I am using is Windows 7.

I have reproduced this error with other computers and configurations, but curiously it seems that nobody else is facing it.

Upvotes: 0

Views: 207

Answers (1)

greg-449
greg-449

Reputation: 111217

The setMenu method sets the pop-up menu for any control. The menu style for this must be SWT.POP_UP.

You are probably looking for the setMenuBar method of Shell which sets to menu bar at the top of the window (top of the screen on macOS). This requires the menu style to be SWT.BAR.

Upvotes: 1

Related Questions