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.
Here are a few examples of what you can do with NSMenuFX.
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);Create common macOS menus like the Window menu.
// Create the window menu
var windowMenu = new Menu("Window");
// ...
// Add your own menu items
// Automatically add windows
MenuToolkit.toolkit().autoAddWindowMenuItems(windowMenu);Create a dock icon menu. Note that images for menu items in dock menus are not supported by macOS.
// Create the dock menu
var menu = new Menu("Window");
// ...
// Add your own menu items
// Set the dock menu
MenuToolkit.toolkit().setDocIconMenu(menu);Add a tray menu. Pass null to remove the tray menu again.
// Create the tray menu
var menu = new Menu("Window");
// ...
// Add your own menu items
// Set the try menu
MenuToolkit.toolkit().setTrayMenu(menu);Use the native context menu instead of a JavaFX based context menu.
// Create the context menu
var 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 appearance, you can switch between LIGHT and DARK mode, or use AUTO to adapt the
appearance of macOS.
// Set appearance automatically (or manually to DARK/LIGHT)
MenuToolkit.toolkit().setAppearanceMode(AppearanceMode.AUTO);- Quickly create an "About" menu
- Automatically use the same menu bar for all stages
To find more examples, check out the sample applications here.
Add the following lines to the repositories in your pom.xml
<repository>
<id>railroad</id>
<name>Railroad Repository</name>
<url>https://maven.railroadide.dev/releases</url>
</repository>Add the following lines to the dependencies in your pom.xml
<dependency>
<groupId>dev.railroadide</groupId>
<artifactId>nsmenufx</artifactId>
<version>1.0.0</version>
</dependency>Add the following lines in your build.gradle
repositories {
maven { url "https://maven.railroadide.dev/releases" }
}
dependencies {
implementation "dev.railroadide:nsmenufx:1.0.0"
}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.





