user377628
user377628

Reputation:

Menu doesn't show up with SWT on Mac OS X

For some reason, the menu doesn't show up at all when I run my simple SWT application on OS X. This problem doesn't happen with other SWT applications that I've written, and I'm not sure what I'm doing differently. When I run my program, the menu I see at the top of the screen belongs to Eclipse, which happens to be the IDE I'm using. Also, while the Eclipse menu is visible, it is not clickable or responsive in any way.

Here's what the menu looks like now (to avoid confusion as to what I meant):

Eclipse's menu

Here's the relevant code:

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.ToolItem;
import org.eclipse.wb.swt.SWTResourceManager;

public class AnalyzerApp {

    protected Shell shell;

    Display display;

    boolean thisIsAMac = SWT.getPlatform().equals("cocoa");

    public static void main(String[] args) {
        try {
            AnalyzerApp window = new AnalyzerApp();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void open() {
        Display.setAppName("Analyzer");
        display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }


    protected void createContents() {
        shell = new Shell();
        shell.setSize(832, 526);
        shell.setText("Analyzer");
        shell.setLayout(new FormLayout());

        // Menu

        Menu menu = new Menu(shell, SWT.BAR);
        shell.setMenuBar(menu);
        if(thisIsAMac) menu = display.getMenuBar();
            // ^ tried to fix with this, didn't change anything

        MenuItem mntmFile = new MenuItem(menu, SWT.CASCADE);
        mntmFile.setText("File");

        Menu fileMenu = new Menu(mntmFile);
        mntmFile.setMenu(fileMenu);

        MenuItem mntmNew = new MenuItem(fileMenu, SWT.NONE);
        mntmNew.setText("New");

        MenuItem mntmOpen = new MenuItem(fileMenu, SWT.NONE);
        mntmOpen.setText("Open");

        MenuItem mntmSave = new MenuItem(fileMenu, SWT.NONE);
        mntmSave.setText("Save");

        MenuItem mntmSaveAs = new MenuItem(fileMenu, SWT.NONE);
        mntmSaveAs.setText("Save As");

        new MenuItem(fileMenu, SWT.SEPARATOR);

        MenuItem mntmImport = new MenuItem(fileMenu, SWT.NONE);
        mntmImport.setText("Import");


    }
}

Upvotes: 1

Views: 1836

Answers (3)

mrzzmr
mrzzmr

Reputation: 903

Still a problem. But works if you open a window of any other application after starting your SWT app, then switch back. A hack for doing this programmatically from your SWT app is to open a Finder window after getting your Display instance:

Display display = Display.getDefault();
try { 
     Runtime.getRuntime().exec("open .");
} catch (Exception e) { }

Upvotes: 0

Oliver
Oliver

Reputation: 249

I have exactly the same problem on Mavericks with Eclipse 4.4 but I realized that I can see the menu bar when I but the swt application in the background and bring it to the front again. Still, seems to be some kind of bug.

Upvotes: 2

Arless
Arless

Reputation: 462

I have the same problem using IntelliJ and when I run from the command line, the only way that I found to show the app menu is creating an .app directory (http://www.eclipse.org/swt/macosx/) and executing it with double click or using the open command.

Upvotes: 1

Related Questions