从JavaFX打开外部应用程序

问题描述 投票:2回答:3

我找到了一种使用HostServices在默认浏览器上打开链接的方法。

getHostServices().showDocument("http://www.google.com");
  • 有没有办法在默认媒体播放器中打开媒体?
  • 有没有办法启动特定的文件或应用程序?
java javafx-8 external-application
3个回答
2
投票

如果要在浏览器中打开具有http:方案的URL,或者使用该文件类型的默认应用程序打开文件,则引用的HostServices.showDocument(...)方法提供了“纯JavaFX”方法来执行此操作。请注意,您无法使用此功能(据我所知)从Web服务器下载文件并使用默认应用程序打开它。

要使用默认应用程序打开文件,必须将文件转换为file: URL的字符串表示形式。这是一个简单的例子:

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class OpenResourceNatively extends Application {

    @Override
    public void start(Stage primaryStage) {
        TextField textField = new TextField("http://stackoverflow.com/questions/39898704");
        Button openURLButton = new Button("Open URL");
        EventHandler<ActionEvent> handler = e -> open(textField.getText());
        textField.setOnAction(handler);
        openURLButton.setOnAction(handler);

        FileChooser fileChooser = new FileChooser();
        Button openFileButton = new Button("Open File...");
        openFileButton.setOnAction(e -> {
            File file = fileChooser.showOpenDialog(primaryStage);
            if (file != null) {
                open(file.toURI().toString());
            }
        });

        VBox root = new VBox(5, 
                new HBox(new Label("URL:"), textField, openURLButton),
                new HBox(openFileButton)
        );

        root.setPadding(new Insets(20));
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
    }

    private void open(String resource) {
        getHostServices().showDocument(resource);
    }

    public static void main(String[] args) {
        launch(args);
    }
}

4
投票

一般来说,您可以使用Desktop#open(file)本机打开文件,如下所示:

final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.OPEN)) {
    desktop.open(file);
} else {
    throw new UnsupportedOperationException("Open action not supported");
}

启动关联的应用程序以打开文件。如果指定的文件是目录,则启动当前平台的文件管理器以将其打开。

更具体地说,如果是浏览器,您可以直接使用Desktop#browse(uri),如下所示:

final Desktop desktop = Desktop.isDesktopSupported() ? Desktop.getDesktop() : null;
if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) {
    desktop.browse(uri);
} else {
    throw new UnsupportedOperationException("Browse action not supported");
}

启动默认浏览器以显示URI。如果默认浏览器无法处理指定的URI,则会调用为处理指定类型的URIs而注册的应用程序。应用程序是根据URI类定义的URI的协议和路径确定的。如果调用线程没有必要的权限,并且这是从applet中调用的,则使用AppletContext.showDocument()。同样,如果调用没有必要的权限,并且这是从Java Web Started应用程序中调用的,则使用BasicService.showDocument()


1
投票

只有java.awt.Desktop的解决方案才能让我从JavaFX打开一个文件。

但是,起初,我的应用程序卡住了,我必须弄清楚是否有必要从一个新线程调用Desktop#open(File file)。从当前线程或JavaFX应用程序线程Platform#runLater(Runnable runnable)调用该方法导致应用程序无限期挂起而不会抛出异常。

这是一个带有工作文件开放解决方案的小样本JavaFX应用程序:

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import javafx.application.Application;
import javafx.concurrent.Task;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.FileChooser;
import javafx.stage.Stage;

public class FileOpenDemo extends Application {

    @Override
    public void start(Stage primaryStage) {
        final Button button = new Button("Open file");

        button.setOnAction(event -> {
            final FileChooser fileChooser = new FileChooser();
            final File file = fileChooser.showOpenDialog(primaryStage.getOwner());

            if (file == null)
                return;

            System.out.println("File selected: " + file.getName());

            if (!Desktop.isDesktopSupported()) {
                System.out.println("Desktop not supported");
                return;
            }

            if (!Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
                System.out.println("File opening not supported");
                return;
            }

            final Task<Void> task = new Task<Void>() {
                @Override
                public Void call() throws Exception {
                    try {
                        Desktop.getDesktop().open(file);
                    } catch (IOException e) {
                        System.err.println(e.toString());
                    }
                    return null;
                }
            };

            final Thread thread = new Thread(task);
            thread.setDaemon(true);
            thread.start();
        });

        primaryStage.setScene(new Scene(button));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

javafx.application.HostServices提出的另一个解决方案根本不起作用。我在Ubuntu 17.10 amd64上使用OpenJFX 8u141并且在调用HostServices#showDocument(String uri)时遇到以下异常:

java.lang.ClassNotFoundException: com.sun.deploy.uitoolkit.impl.fx.HostServicesFactory

显然,JavaFX HostServices还没有在所有平台上正确实现。关于这个主题,请参阅:https://github.com/Qabel/qabel-desktop/issues/420

© www.soinside.com 2019 - 2024. All rights reserved.