Java Swing - 如何在Mac上双击项目文件以打开我的应用程序并加载文件? [英] Java Swing - How to double click a project file on Mac to open my application and load the file?

查看:115
本文介绍了Java Swing - 如何在Mac上双击项目文件以打开我的应用程序并加载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Mac Java Swing应用程序,并在Info.plist文件中为它设置了文件扩展名(* .pkkt),因此当双击该文件时,它会打开我的应用程序。

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.

当我这样做时程序运行正常。现在我需要在程序中加载(* .pkkt)项目,但文件路径不作为参数传递给Mac中的main(...)方法,就像在Windows操作系统中那样。

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.

经过一番搜索,我找到了一个Apple处理罐 MRJToolkitStubs 具有 MRJOpenDocumentHandler 界面来处理这些点击的文件。我已尝试通过在主程序类中实现该接口来加载该文件,但它无法正常工作。在程序启动时永远不会调用实现的方法。

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 ?

-------------------------------------------------编辑:添加代码示例

------------------------------------------------- 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;
    }
}

正如上面评论中提到的getProjectFilePath ()总是空的!

推荐答案

在Java 9上,使用 Desktop.setOpenFileHandler( )



专有的 com.apple.eawt 包已经过已从最新版本的Java中删除,并已合并到 Desktop 类中的各种方法中。对于您的具体示例:

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
        }
    }
}

然后在其他地方添加:

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

OpenFilesEvent 类还有一个 getSearchTerm( ) 方法。假设一个人在macOS上使用Spotlight来搜索单词StackOverflow,然后决定打开一个文档。使用此方法,您可以确定StackOverflow是他们搜索的单词,并选择对其执行某些操作(可能会突出显示该单词的第一个出现位置)。

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).

这篇关于Java Swing - 如何在Mac上双击项目文件以打开我的应用程序并加载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆