Java Swing - 如何在Mac上双击项目文件以打开我的应用程序并加载文件?

时间:2012-05-11 06:24:24

标签: java macos swing

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

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

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

此界面如何运作?

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

以下是我正在使用的代码:

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()”始终为空!

2 个答案:

答案 0 :(得分:0)

您将要使用Apple Java Extensions

它们应该包含在Mac OS X上运行的任何JDK中,但文档很难获得。有关详细信息,请参阅this answer

具体来说,您需要制作OpenFilesHandeler

此代码段应该有效:

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
    }

}

某处:

import com.apple.eawt.Application;

...

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

答案 1 :(得分:0)

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

专有/** * Copyright 2017 Yahoo, Inc. * Zlib license: https://www.zlib.net/zlib_license.html */ package me.klotz.spark.utils import org.apache.spark.sql.functions._ import org.apache.spark.sql.Dataset import org.apache.spark.sql.Row import org.apache.spark.util.sketch.BloomFilter import org.apache.spark.SparkContext object BloomFilterEnhancedJoin { // not parameterized for field typel; assumes string /** * Like .join(bigDF, smallDF, but accelerated with a Bloom filter. * You pass in a size estimate of the bigDF, and a ratio of acceptable false positives out of the expected result set size. * ratio=1 is a good start; that will result in about 50% false positives in the big-small join, so the filter accepts * about as many as it passes, rather than rejecting almost all. Pass in a size estimate of the big dataframe * to avoid enumerating it. The small DataFrame gets enumerated anyway. * * Example use: * <code> * import me.klotz.spark.utils.BloomFilterEnhancedJoin._ * val (dups_joined, bloomFilterBroadcast) = df_big.joinBloom(1024L*1024L*1024L, dups, 10.0, "id") * dups_joined.write.format("orc").save("dups") * bloomFilterBroadcast.unpersist * <code> */ implicit class BloomFilterEnhancedJoiner(bigdf:Dataset[Row]) { /** * You should call bloomFilterBroadcast.unpersist after */ def joinBloom(bigDFCountEstimate:Long, smallDF: Dataset[Row], ratio:Double, field:String) = { val sc = smallDF.sparkSession.sparkContext val smallDFCount = smallDF.count val fpr = smallDFCount.toDouble / bigDFCountEstimate.toDouble / ratio println(s"fpr=${fpr} = smallDFCount=${smallDFCount} / bigDFCountEstimate=${bigDFCountEstimate} / ratio=${ratio}") val bloomFilterBroadcast = sc.broadcast((smallDF.stat.bloomFilter(field, smallDFCount, fpr))) val mightContain = udf((x: String) => if (x != null) bloomFilterBroadcast.value.mightContainString(x) else false) (bigdf.filter(mightContain(col(field))).join(smallDF, field), bloomFilterBroadcast) } } } 软件包已从最新版本的Java中删除,并已合并到com.apple.eawt类的各种方法中。对于您的具体示例:

Desktop

然后在其他地方添加:

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