Skip to content

0x4a616e/NSMenuFX

 
 

Repository files navigation

NSMenuFX

Codacy Badge Maven Central Test CodeQL Known Vulnerabilities JFXCentral

A simple library to customize the macOS menu bar to give your JavaFX app a more native look and feel.

NSMenuFX provides a mapping layer between the JavaFX Menu and AppKits NSMenu objects. It uses JNA to directly set the menus for your application using macOS native API.

Features

Here are a few examples of what you can do with NSMenuFX.

Application menu

Custom App Menu Screenshot

Customize the auto-generated application menu of your JavaFX app.

// Create the default Application menu
Menu defaultApplicationMenu = tk.createDefaultApplicationMenu("test");

// Update the existing Application menu
MenuToolkit.toolkit().setApplicationMenu(defaultApplicationMenu);

Window menu

Custom App Menu Screenshot

Create common macOS menus like the Window menu.

// Create the window menu
Menu windowMenu = new Menu("Window");
// ...
// Add your own menu items

// Automatically add windows
MenuToolkit.toolkit().autoAddWindowMenuItems(windowMenu);

Dock menu

Custom App Menu Screenshot

Create a dock icon menu. Note that images for menu items in dock menus are not supported by macOS.

// Create the dock menu
Menu menu = new Menu("Window");
// ...
// Add your own menu items

// Set the dock menu
MenuToolkit.toolkit().setDocIconMenu(menu);

Tray menu

Custom App Menu Screenshot

Add a tray menu. Pass null to remove the tray menu again.

// Create the tray menu
Menu menu = new Menu("Window");
// ...
// Add your own menu items

// Set the try menu
MenuToolkit.toolkit().setTrayMenu(menu);

Context menu

Custom App Menu Screenshot Custom App Menu Screenshot

Use the native context menu instead of a JavaFX based context menu.

// Create the context menu
Menu menu = new Menu();
// ...
// Add your own menu items

// Show the context menu when right-clicking the stage
scene.setOnMouseClicked(event ->
{
  if (event.getButton() == MouseButton.SECONDARY) {
    MenuToolkit.toolkit().showContextMenu(context, event);
  }
});

To adapt the context menu appearence, you can switch between LIGHT and DARK mode, or use AUTO to adapt the appearence of macOS.

// Set appearance automatically (or manually to DARK/LIGHT)
MenuToolkit.toolkit().setAppearanceMode(AppearanceMode.AUTO);

And more

  • Quickly create an "About" menu
  • Automatically use the same menu bar for all stages

To find more examples, check out the sample applications here.

Maven

Add the following lines to the dependencies in your pom.xml

<dependency>
    <groupId>de.jangassen</groupId>
    <artifactId>nsmenufx</artifactId>
    <version>3.1.0</version>
</dependency>

Gradle

Add the following line to the dependencies in your build.gradle

compile "de.jangassen:nsmenufx:3.1.0"

Migrating from 2.1

For most parts, the API has not changed. The most prominent difference is that the package name has changed from de.codecentric.centerdevice to de.jangassen to match the new maven coordinates. Also, the About dialog no longer uses a WebView, so it can either display plain text or a list of Text objects. If you want to continue using a WebView, you have to create one and pass that to the AboutStageBuilder:

WebView webView = new WebView();
webView.getEngine().loadContet("<b>Credits</b>");

AboutStageBuilder.start("My App").withNode(webView).build();

Known issues

There is a known issue with OpenJFX that may cause the menu bar to be unresponsive after launch. You can find more details about this issue in the respective OpenJFX bug ticket. A pull request containing a fix has already been merged and should be available in OpenJFX 16+.

NSMenuFX no longer supports changing the title of the application menu at runtime. This has always been a bit "hacky" as it is not really supported by macOS. As a result, the new name was no longer bold faced when it was changed with previous versions of NSMenuFX.

To set the title of the application menu to the name of your application, you need to bundle the application and set CFBundleName in Info.plist.

About

Full macOS menu bar access for JavaFX apps

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Java 99.5%
  • CSS 0.5%