Missing OpenFilesEvent for JavaFX on Mac

断了今生、忘了曾经 提交于 2019-12-12 17:13:39

问题


I have a JavaFX 8 desktop application and I'm creating an .app application bundle to distribute the application to Mac users. I use the Oracle “Self-Contained Application Packaging” tool to generate the bundle.

The problem that I have relates to the files associated with my application. I am associating the extension .wordy with these files. If I have the application open and I double click one of these files in the Mac Finder, my application receives an OpenFilesEvent containing the path to the file and everything works perfectly. If, however, the application is not open, double clicking a .wordy file in the Finder opens my application as I would expect but I never receive the event containing the path to the file that the user double-clicked on.

The file association is done in the Ant script for the Oracle “Self-Contained Application Packaging” tool, as follows:

<project name="VocabHunter Packaging" basedir=""
         xmlns:fx="javafx:com.sun.javafx.tools.ant">
    ...
    <fx:info title="VocabHunter">
        <fx:association description="VocabHunter session"
                        extension="wordy"
                        mimetype="application/x-vnd.VocabHunterSession"
                        icon="${basedir}/icons/mac/VocabHunterSession.icns"/>
    </fx:info>
    ...
</project>

In the Java code, I obtain an instance of com.apple.eawt.Application and then register the listener for the OpenFilesEvent as follows:

Application application = Application.getApplication();
application.setOpenFileHandler(new OsxOpenFilesHandler(listener));

You can see the full code here.

Does anyone know how to fix this so that I receive an event containing the path to the .wordy file even if the application was not running at the moment that the file was double clicked?

In the interests of completeness, I'm using the Oracle JDK 1.8.0_66 for Mac.


回答1:


I tested with your code and also met this problem. But when I used code directly in start(Stage primaryStage) method in to listen like this:

Application lowLevelApp = com.sun.glass.ui.Application.GetApplication();
lowLevelApp.setEventHandler {...}

I can get the OpenFilesEvent when first double clicked on file.




回答2:


There is an entry in the bug database for this issue. https://bugs.openjdk.java.net/browse/JDK-8187992




回答3:


You probably call the application.setOpenFileHandler() code too late during the application initialization. Try calling it as early as possible in main() and see if that solves the problem. I am not sure exactly when Mac OS X passes the OpenFile event to Java, but if at that time you haven't prepared by calling application.setOpenFileHandler() then the event will get lost.




回答4:


For future reference: getting the openFileHandler to work correctly can be very tricky. The handler is invoked from an event handler on the UI event thread, which means there is no guarantee that main() has completed when the handler is run. For best results, the openFileHandler should be set up using a static initializer, and main() and the open file handler should both invoke the same initialization code on the UI event thread, and the initialization code should be written to work if called more than once.



来源:https://stackoverflow.com/questions/34569389/missing-openfilesevent-for-javafx-on-mac

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