0
votes

I created a simple window using JFace's ApplicationWindow, and tried adding a menu bar.

public MainWindow() {
    super(null);
    this.addMenuBar(); // Enable menu-bar.
}

@Override
protected Control createContents(Composite parent) {
    MenuManager menuBar = this.getMenuBarManager();

    // Create "File" menu.
    MenuManager fileMenu = new MenuManager("&File");
    menuBar.add(fileMenu);

    // Create "File" > "Test" action.
    Action testAction = new Action("&Test") {
        @Override
        public void run() {
            System.out.println("Test");
        }
    };
    fileMenu.add(testAction);

    // Create contents.
    Label text = new Label(parent, SWT.NONE);
    text.setText("Lorem ipsum.");

    return parent;
}

I must be overlooking something simple because it doesn't appear to work. The menu-bar simply does not show up. What am I doing wrong? (Please note that I have the global menu disabled in Ubuntu).

Test Window

NOTE: I've tested this on Ubuntu 15.10 with GTK 3.16.7, and Arch Linux with GTK 3.22.18, both with SWT 3.105.3 and JFace 3.12.2.

1

1 Answers

1
votes

Setting up the menu bar in createContents is too late, you need to do this earlier. One way is to override createMenuManager:

@Override
protected MenuManager createMenuManager()
{
  MenuManager menuBar = new MenuManager();

  // Create "File" menu.
  MenuManager fileMenu = new MenuManager("File");
  menuBar.add(fileMenu);

  // Create "File" > "Test" action.
  Action testAction = new Action("&Test")
   {
     @Override
     public void run()
     {
       System.out.println("Test");
     }
   };
  fileMenu.add(testAction);

  return menuBar;
}

@Override
protected Control createContents(Composite parent)
{
  // Create contents.
  Label text = new Label(parent, SWT.NONE);
  text.setText("Lorem ipsum.");

  return text;
}