双击文件将参数传递给JavaFx Application [英] Pass Parameters to JavaFx Application by double-click on file

查看:123
本文介绍了双击文件将参数传递给JavaFx Application的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个JavaFX应用程序,部署了.app文件,它运行正常。然后我设置操作系统打开所有带有我的应用程序的特定扩展名的文件。我的问题是,当我双击一个文件时,我的应用程序打开了,但我不知道打开它的文件是哪个。

I created a JavaFX application, deployed the .app file and it works fine. I then set the operative system to open all the files with a specific extension with my application. My problem is that when I double-click on a file I get my application to open, but I don't know which is the file that opened it.

我试过使用函数 getParameters()。getRaw()检查应用程序参数,但它总是返回一个空列表。

I tried to check the application parameters with the function getParameters().getRaw() but it always returns an empty List.

有人知道如何检索打开应用程序的文件的路径吗?

Does anybody know how to retrieve the path to the file that opened the application?

推荐答案

我终于找到了解决这个问题的方法。

I finally found a way to solve this.

为了回答这个问题,我创建了一个由三个类组成的示例应用程序:

To answer this I created a Sample Application that is composed by three classes:

Launcher
MyApp
Handler_OpenFile

MyApp是扩展的类在javafx.application.Application类中,Handler_OpenFile用于处理双击事件,最后Launcher是包含main的那个。

MyApp is the class extending the javafx.application.Application class, the Handler_OpenFile is used to handle the double-click event and finally the Launcher is the one containing the main.

Launcher.java:this class必须存在,因为如果main是在扩展javafx.ap的类中定义的plication.Application OpenFilesEvent将无法正常工作(更确切地说,只有在应用程序已经打开时才会触发OpenFilesEvent)。

Launcher.java: this class MUST exist because if the main was defined inside the class that extends the javafx.application.Application the OpenFilesEvent won't work properly (more precisely the OpenFilesEvent will be fired only if the application was already opened).

public class Launcher {
    public static void main(String[] args) {
        if (System.getProperty("os.name").contains("OS X")){
            com.apple.eawt.Application a = com.apple.eawt.Application.getApplication();
            Handler_OpenFile h_open = new Handler_OpenFile();
            a.setOpenFileHandler(h_open);
            Application.launch(Main.class, args);
        }
    }
}

Handler_OpenFile.java:此类定义一个静态变量,用于存储打开应用程序的File的值。这可能不是最好的解决方案,但它是我现在可以使它工作的唯一方法。

Handler_OpenFile.java: this class defines a static variable that stores the value of the File that opened the application. This isn't probably the best solution but it's the only way I could make it work for now.

public class Handler_OpenFile implements OpenFilesHandler {
    public static File file = null;

    @Override
    public void openFiles(OpenFilesEvent e) {
        for (File file : e.getFiles()){
            this.file = file;      
        }
    }
}

MyApp.java:此类将能够访问Handler_OpenFile类中指定的静态值并检索打开文件的绝对路径。

MyApp.java: this class will be able to access the static value assigned in the Handler_OpenFile class and retrieve the opening file's absolute path.

public class MyApp extends Application {
    @Override
    public void start(Stage primaryStage) {

        Logger logger = Logger.getLogger("log");  
        FileHandler fh;  

        try {  
            // This block configure the logger with handler and formatter  
            fh = new FileHandler("../Logfile.log");  
            logger.addHandler(fh);
            SimpleFormatter formatter = new SimpleFormatter();  
            fh.setFormatter(formatter);  

            // the following statement is used to log any messages  
            logger.info("Application launched from: " + Handler_OpenFile.file.getAbsolutePath());  

        } catch (SecurityException | IOException exception) {  
            exception.printStackTrace();  
        } 


        try {
            BorderPane root = new BorderPane();
            Scene scene = new Scene(root,400,400);
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

最后在build.xml中创建捆绑包的文件必须添加文件关联(在此示例中为扩展名为.zzz的文件):

finally in the build.xml file that creates your bundle you will have to add the file association (in this example with files with a .zzz extension):

<fx:info title="Sample" vendor="me">
    <fx:association extension="zzz" description="Sample Source"/>
</fx:info>

这只适用于Java的最后一次更新(8u40):此链接上的文档。对于以前的版本,您必须在包中手动更改info.plist,就像在 Apple Java Extensions文档

this will work only with the last update of Java(8u40): documentation at this link. For previous versions you will have to change the info.plist manually inside your bundle like it is said in the Apple Java Extensions documentation.

这篇关于双击文件将参数传递给JavaFx Application的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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