RFR: 8359108: Mac - When Swing starts First, native application menu doesn't work for JavaFX
Martin Fox
mfox at openjdk.org
Thu Jun 19 17:43:30 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
Any fix that happens inside Glass is the wrong fix.
I think I have an idea of what’s happening under the hood with this PR. When a JavaFX window loses focus JavaFX removes most of the items from NSApp.mainMenu. I suppose when the Swing window gains focus Swing is re-writing most of NSApp.mainMenu without JavaFX knowing about it. And the same thing is happening when focus moves from a Swing window to a JavaFX window. So window focus is being used as an informal protocol to determine whether Swing or JavaFX controls *most* of the system menu.
This will all break down with the application menu, the first menu in the menu bar after the Apple logo menu. That one requires special handling since it must be present at all times. JavaFX sets that up once and never touches it again. I don’t know how Swing handles this menu but I’m guessing it does something similar. It looks like this PR is removing the Swing items from the application menu so JavaFX can re-populate it. Or something like that. It doesn’t matter.
If you want Swing and JavaFX to coordinate on the system menu there needs to be a formal protocol for doing so and that formal protocol must take into account the special needs of the application menu. Neither system should be messing around at the platform level adding and removing the other guy’s menu items. JavaFX is not designed to support that sort of manipulation and I doubt Swing is either. If it works it’s entirely by accident.
-------------
PR Comment: https://git.openjdk.org/jfx/pull/1835#issuecomment-2988787155
More information about the openjfx-dev
mailing list