Closed Glavo closed 3 years ago
Oh, I solved this problem by adding Add-Exports: javafx.graphics/com.sun.javafx.tk
to MANIFEST.MF in my application. Can this problem be solved in NSMenuFX?
AFAIK, this approach will only work for the "main" jar, so it won't have any effect when added to NSMenuFX. So I'm afraid this cannot be easily solved in NSMenuFX, at least not by adding exports.
It would be nice to see a NSMenuFX update for Java 11+. It would be fine with me, by-the-way if it only supported Java 11 and later. I say this because I intend to always deliver JavaFX apps with bundled runtimes and am currently migrating to Java 11 and will likely move to newer versions rapidly.
I too am attempting to migrate an application to Java 11, and have run into this same problem. It would be great if NSMenuFX could be somehow updated to support Java11+.
I guess the only way around this is to ship own native bindings to get direct access to the mac menu bar. This will also give us way more cool opportunities like adding a menu if the app has no active window or customising the menu on the app icon in the dock. I already have a more or less working version ready but it still requires some small adjustments to make it fully compatible with the old version. I think all required objective-c code has been written, so all that's missing is some java glue code. If anyone wants to speed this up a bit, help is always welcome ;)
The biggest issue i'm currently aware of that needs some solution is when the menu should/can be set. If it is set too early, it'll get overwritten again by the JavaFX default one. I tried using WindowEvent.WINDOW_SHOWN
but unfortunately that event is fired just before the window becomes visible. When I set the menu after receiving this event, the menu bar gets replaced again. For testing, i'm just using a small delay after receiving the event before setting the menu, but that does not seem like a good solution. If anyone has a good idea how to fix this (preferably without additional native bindings), i'd be happy to hear it :)
@0x4a616e Are you still working on this, or have you dropped it?
@0x4a616e
@0x4a616e
- Concerning the placement of the menu creation, I had some success with wrapping the menu creation into a Platform.runlater call but I do not know whether this works in general or just for me.
- Did you ever consider to fix the menu problems directly inside JavaFX. Nowadays this is not so difficult anymore and this sounds like the cleanest solution to me.
Thanks for the quick reply.
My reply was addressing @0x4a616e previous answer but you are of course also welcome to step in :-)
Whoopsie :D
Unfortunately I haven't been able to find a solution for the aforementioned timing issue. Also I have to admit I don't have as much time as I'd like to have to address this problem. If someone finds a solution on when to overwrite the JavaFX default menu bar, i'll try to implement that in a timely manner. I already tried stackoverflow but people there also don't seem to have a good idea...
I finally got this working for my application. Through various trials, and checking the error messages, I found I needed to add the following --add-opens
the the jvmArg:
"--add-opens", "javafx.graphics/com.sun.javafx.tk.quantum=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.javafx.tk=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.glass.ui=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.glass.ui.mac=centerdevice.nsmenufx",
"--add-opens", "javafx.graphics/com.sun.javafx.menu=centerdevice.nsmenufx",
"--add-opens", "javafx.controls/com.sun.javafx.scene.control=centerdevice.nsmenufx"
I realize this is a bit of a work around to get around the IllegalAccessError
, but it works (note: tried with Java13 and javaFX13)
@vewert thanks for sharing!
With the tips from this issue, I was able to build a sample "Hello World" app into a standalone native-looking macOS app using jpackage
via the Badass JLink Gradle Plugin I'm using Github Actions to do a matrix build and it works when built/bundled with both OpenJDK 11 and OpenJDK 13. You can view the build and download/test the artifacts here: https://github.com/ConsensusJ/SupernautFX/actions/runs/38586288
And here's the commit that added NSMenuFX: https://github.com/ConsensusJ/SupernautFX/commit/ab7b18a62a7a7bd24b1250273394127d78d19668
Please refer to https://github.com/codecentric/NSMenuFX/issues/36 for recent updates. Those type of issues should be solved with the new major upgrade.
Closing this as obsolete as i just published
<dependency>
<groupId>de.jangassen</groupId>
<artifactId>nsmenufx</artifactId>
<version>3.0.0</version>
</dependency>