RFR: 8359108: Mac - When Swing starts First, native application menu doesn't work for JavaFX
Martin Fox
mfox at openjdk.org
Tue Jun 24 16:05:36 UTC 2025
On Wed, 18 Jun 2025 20:58:20 GMT, Pabulaner IV <duke at openjdk.org> wrote:
> This pull request fixes the system menu bar on Mac when combining windows of Swing and JavaFX.
>
> The first issue was to get the native menu bar working simultaneously on Swing and JavaFX, which was done by just returning always true inside the supportsSystemMenu method.
>
> The second issue was to remove all system menu items installed by a swing window. This was fixed by checking the system menu bar every time an item is inserted or removed and removing all menu items that are not owned by JavaFX. This check is done on every insert and remove as JavaFX does not have a clear method inside the MenuBarDelegate class that could be called every time the window gets the focus.
>
> I tested the fix with two Swing and two JavaFX windows that are run inside the same application and it works without any errors.
>
> Co-Author: @FlorianKirmaier
The interface you want to look at is TKSystemMenu. When JavaFX wants to install a set of Menus in the system menu this is the interface it uses.
The best way to do this is to take the menus passed into TKSystemMenu, create Swing equivalents, and then install them in a Swing menubar. That way only Swing is manipulating the system menu. Unfortunately this is a lot of work in part because it's not a one-time conversion. The system menu machinery is expected to track changes made to the JavaFX menus so there are a lot of delegates and listeners involved. Fortunately there's a working example in GlassSystemMenu you can use for reference. I haven't looked into the threading issues involved here (and I'm sure there are some).
I don't think it's a good idea to try to switch control of NSApp.mainMenu between JavaFX and Swing. Under the hood they have very different ways of manipulating the mainMenu and their own assumptions about how the application menu works. And frankly you should never allow JavaFX to show its application menu in a Swing app since the JavaFX version has serious limitations. I'm working on fixing that but for now it's best that Swing is in charge of the system menu.
-------------
PR Comment: https://git.openjdk.org/jfx/pull/1835#issuecomment-3001072715
More information about the openjfx-dev
mailing list