Prevent JavaFX thread from dying with JFXPanel Swing interop?

杀马特。学长 韩版系。学妹 提交于 2019-11-30 09:35:34

This is a really old "bug" that was somewhat fixed with the introduction of Platform.setImplicitExit(false). You can read the developers comments in the open issue JDK-8090517. As you will see it has a low priority and probably will never get fixed (at least not soon).

Another solution you might want to try instead of using Platform.setImplicitExit(false) is to extend the Application class in your current Main class and use the primary Stage to display the application's main window. As long as the primary Stage remains open the FX Thread will be alive (and dispose correctly when you close your app).

If you aren't looking to use an FX Stage as your main window (since it would require to use a SwingNode for what you have now or migrate your UI to JavaFX) you can always fake one like this:

@Override
public void start(Stage primaryStage) throws Exception {
    YourAppMainWindow mainWindow = new YourAppMainWindow();
    // Load your main window Swing Stuff (remember to use 
    // SwingUtilities.invokeLater() to run inside the Event Dispatch Thread
    mainWindow.initSwingUI();

    // Now that the Swing stuff is loaded open a "hidden" primary stage
    // that will keep the FX Thread alive
    primaryStage.setWidth(0);
    primaryStage.setHeight(0);
    primaryStage.setX(Double.MAX_VALUE);
    primaryStage.setY(Double.MAX_VALUE);
    primaryStage.initStyle(StageStyle.UTILITY);
    primaryStage.show();
}

Keep in mind that faking a primary stage (or migrating your main window to FX) will end in more code than simply using Platform.setImplicitExit(false) and Platform.exit() accordingly.

Anyway, hope this helps!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!