English 中文(简体)
Java Swing——如何在Mac上重复点击项目档案,以开放我的申请并装上档案?
原标题:Java Swing - How to double click a project file on Mac to open my application and load the file?

我提出了Mac Java Swing的申请,我已在“Info.plist”案卷中为其设定了档案延期(*.pkkt),这样,在翻一番地点击该档案时,我就开始申请。

如果是的话,该方案将处以罚款。 现在需要在该方案中装上(*.pkkt)项目,但档案途径并没有像Windows操作系统那样作为向Mac的主要(......)方法的论据。

在进行了一些搜索后,发现了一个 Apple果处理,即“,有MRJOpenDocumentHandler接口处理这种被点击的档案。 我试图通过在主要方案类别中执行该接口来装载这一档案,但这项工作没有进行。 在方案启动时,从未采用已执行的方法。

<><>>>> 这一接口如何运行?

页: 1 Edit: Add a Code Sample

这里使用的是:

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 !

问题回答

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());

<代码>OpenFiles 活动 班级还有getSearchTerm(。 一个人利用Spotlight on macOS搜索“StackOverflow”一词,然后决定开放文件。 有了这一方法,你可以确定,“StackOverflow”是他们搜索的字,选择做些什么(或许可以突出一字)。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签