codecentric / NSMenuFX

Other
128 stars 27 forks source link

MenuToolkit returns null under MacOSX #40

Closed fujohnwang closed 3 years ago

fujohnwang commented 3 years ago

I am developing under Mac, when I create a simple demo javafx application, it's ok.

when I put same logic code into another javafx application, I failed with exception below:

2020-11-21 10:04:11,463 INFO  [main] c.k.keebox.KeeboxFXApplication$ - Starting KeeboxFXApplication. using Java 15 on fq16.lan with PID 18369 (/Users/fq/workspace.keevol/KeeboxNative/target/classes started by fq in /Users/fq/workspace.keevol/KeeboxNative)
2020-11-21 10:04:11,465 INFO  [main] c.k.keebox.KeeboxFXApplication$ - No active profile set, falling back to default profiles: default
2020-11-21 10:04:12,435 INFO  [main] c.k.keebox.KeeboxFXApplication$ - Started KeeboxFXApplication. in 1.309 seconds (JVM running for 3.933)
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
    at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.NullPointerException: Cannot invoke "de.codecentric.centerdevice.MenuToolkit.createDefaultApplicationMenu(String, javafx.stage.Stage)" because "tk" is null
    at com.keevol.keebox.KeeboxFXApplication.start(KeeboxFXApplication.scala:112)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

the code part is:

    val scene = new Scene(layout)
    scene.getStylesheets.add(getClass.getResource("/css/style.css").toExternalForm)
    stage.setScene(scene)

    val tk = MenuToolkit.toolkit(Locale.getDefault). 
    tk.setApplicationMenu(tk.createDefaultApplicationMenu("KEEBOX", stage)). // <<<<<<--here

    stage.setTitle("KEEBOX")
    stage.getIcons.add(Images.LOGO)
    stage.show()

I debug into the code , found isSupported() returns false and then everything return null.

But it doesn't make sense, and I don't know why. The only difference between these two application is in the latter, I run springboot first and launch javafx second, But I don't think that's culprit for isSupported return false.

any hint?

fujohnwang commented 3 years ago

I trace here which doesn't make sense:

private static AdapterContext createContext() {
    if (!Toolkit.getToolkit().getSystemMenu().isSupported()) {
      return null;
    }
...
fujohnwang commented 3 years ago

https://www.linkedin.com/pulse/using-awt-splash-screen-javafx-osx-bengt-rutisson

I found why, the AWT thing broke it.

I use AWT for system tray ...

0x4a616e commented 3 years ago

Hi, thanks for sharing. Returning null is obviously not so nice there... I'm currently rewriting this mechanism, so hopefully we won't run into situations like this anymore.

0x4a616e commented 3 years ago

You may want to give the latest update a try. Please refer to https://github.com/codecentric/NSMenuFX/issues/36 for detail

0x4a616e commented 3 years ago

Closing this as obsolete as i just published

<dependency>
  <groupId>de.jangassen</groupId>
  <artifactId>nsmenufx</artifactId>
  <version>3.0.0</version>
</dependency>