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
Menu 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
Menu 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
Menu 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
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);
- 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 dependencies in your pom.xml
<dependency>
<groupId>de.jangassen</groupId>
<artifactId>nsmenufx</artifactId>
<version>3.1.0</version>
</dependency>
Add the following line to the dependencies in your build.gradle
compile "de.jangassen:nsmenufx:3.1.0"
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();
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
.