mikec
mikec

Reputation: 1

nsmenufx MenuToolkit.setApplicationMenu producing inconsistent behavior

I have a JavaFX application for Mac and I'm using NSMenuFX to create the menu bar. I create my application menu using MenuToolkit. If I use the method setApplicationMenu the menu displays correctly. For example it says Quit MyApplication instead of Quit com.example.MyApplication. However, I have code that cancels the closing of the application if certain conditions are met. I have an EventHandler on the Stage that handles the on close request. If the application should not close the WindowEvent is consumed. The problem is the application still closes. However, if I do not use setApplicationMenu, the menu does not display correctly (it says Quit com.example.MyApplication) but consuming the WindowEvent does stop the closing of the application. I am using Java 1.8u77. Any ideas on what I'm doing incorrectly? I cannot reproduce this problem in the sample code that comes with NSMenuFX. Below is the code that creates the menu bar.

private void createMenu(VBox appBox) {
    // Create the menubar
    MenuBar menuBar = new MenuBar();
    menuBar.useSystemMenuBarProperty().set(true);
    MenuToolkit tk = MenuToolkit.toolkit();
    String appName = "MyApplication";
    Menu appMenu = new Menu(appName);
    menuBar.getMenus().add(appMenu);
    MenuItem aboutItem = tk.createAboutMenuItem("MyApplication");
    aboutItem.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            AboutDialog aboutDialog = new AboutDialog(null);
            aboutDialog.initOwner(stage);
            aboutDialog.showAndWait();
        }
    });
    appMenu.getItems().add(aboutItem);
    appMenu.getItems().addAll(new SeparatorMenuItem(),
        tk.createHideMenuItem(appName), tk.createHideOthersMenuItem(),
        tk.createUnhideAllMenuItem(),
        new SeparatorMenuItem(), tk.createQuitMenuItem(appName));
    tk.setApplicationMenu(appMenu);
    // Add the File menu
    Menu file = new Menu("File");
    menuBar.getMenus().add(file);
    // Add the Print item
    print = new MenuItem("Print");
    print.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        PrinterJob job = PrinterJob.createPrinterJob();
        if (job != null && job.showPrintDialog(
                lineChart.getChart().getScene().getWindow())) {
            boolean success = job.printPage(lineChart.getChart());
            if (success) {
                job.endJob();
            }
        }
    });
    file.getItems().add(print);
    // Window Menu
    Menu windowMenu = new Menu("Window");
    windowMenu.getItems().addAll(
        tk.createMinimizeMenuItem(), tk.createZoomMenuItem(),
        tk.createCycleWindowsItem(), new SeparatorMenuItem(),
        tk.createBringAllToFrontItem());
    menuBar.getMenus().add(windowMenu);
    tk.autoAddWindowMenuItems(windowMenu);
    tk.setGlobalMenuBar(menuBar);
}

Upvotes: 0

Views: 151

Answers (1)

mikec
mikec

Reputation: 1

NSMenuFX has been modified to correct this problem. Look for version 2.1.4 or higher.

Upvotes: 0

Related Questions