简体   繁体   中英

Java Swing - How to double click a project file on Mac to open my application and load the file?

I have created a Mac Java Swing application, and i have set a file extension(*.pkkt) for it in the "Info.plist" file, so when double clicking that file it opens my application.

When i do that the program runs fine. Now i need to load the (*.pkkt) project in the program, but the file path is not passed as an argument to the main(...) method in Mac as happens in Windows Operating System.

After some search i found an Apple handling jar " MRJToolkitStubs " that has the MRJOpenDocumentHandler interface to handle such clicked files. I have tried using it to load that file by implementing that Interface in the main program class, but it is not working. The implemented method is never called at the program start-up.

How does this Interface run ?

------------------------------------------------- Edit: Add a Code Sample

Here is the code i am using :

 public static void main( final String[] args ) { . . . MacOpenHandler macOpenHandler = new MacOpenHandler(); String projectFilePath = macOpenHandler.getProjectFilePath(); // Always Empty !! } 
class MacOpenHandler implements MRJOpenDocumentHandler {
    private String projectFilePath = ""; 

    public MacOpenHandler () {
        com.apple.mrj.MRJApplicationUtils.registerOpenDocumentHandler(this) ; 
    }

    @Override
    public void handleOpenFile( File projectFile ) { 
        try {
            if( projectFile != null ) {
                projectFilePath = projectFile.getCanonicalPath();
                   System.out.println( projectFilePath );  // Prints the path fine.
            }
        } catch (IOException e) {}  
    }

    public String getProjectFilePath() {
        return projectFilePath;
    }
}

As mentioned in the comment above "getProjectFilePath()" is always Empty !

You're going to want to use the Apple Java Extensions .

They should be included in any JDK that runs on Mac OS X, but the documentation is kind of hard to get. See this answer for more details.

Specifically, you'll want to make an OpenFilesHandeler .

This code snippet should work:

import com.apple.eawt.event.OpenFilesHandeler;
import com.apple.eawt.event.AppEvent;
import java.io.File;
import java.util.List;

class MacOpenHandler implements OpenFilesHandeler {

    @Override
    public void openFiles(AppEvent.OpenFilesEvent e)  { 
        List<File> files = e.getFiles();
        // do something
    }

}

And somewhere:

import com.apple.eawt.Application;

...

MacOpenHandeler myOpenHandeler = new MacOpenHandeler();
Application.getApplication().setOpenFileHandler(myOpenHandeler);

On Java 9, use Desktop.setOpenFileHandler()

The proprietary com.apple.eawt packages have been removed from recent versions of Java and has been incorporated into various methods in the Desktop class. For your specific example:

import java.awt.desktop.OpenFilesHandler;
import java.awt.desktop.OpenFilesEvent;
import java.io.File;
import java.util.List;

public class MyOpenFileHandler implements OpenFilesHandler {

    @Override
    public void openFiles​(OpenFilesEvent e) {
        for (File file: e.getFiles​()) {
            // Do whatever
        }
    }
}

Then elsewhere, add this:

Desktop.getDesktop().setOpenFileHandler(new MyOpenFileHandler());

The OpenFilesEvent class also has a getSearchTerm() method. Say that a person used Spotlight on macOS to search for the word "StackOverflow", then decided to open up a document. With this method, can you determine that "StackOverflow" was the word they searched for, and choose to do something with that (perhaps highlight the first occurrence of the word).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM