从 JavaFX 打开外部应用程序

Ran*_*pto 4 java external-application javafx-8

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

getHostServices().showDocument("http://www.google.com");
Run Code Online (Sandbox Code Playgroud)
  • 有没有办法在默认媒体播放器中打开媒体?
  • 有没有办法启动特定的文件或应用程序?

Nic*_*tto 6

一般来说,您可以使用本机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");
}
Run Code Online (Sandbox Code Playgroud)

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

更具体地说,在浏览器的情况下,您可以直接使用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");
}
Run Code Online (Sandbox Code Playgroud)

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


Jam*_*s_D 5

如果您想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);
    }
}
Run Code Online (Sandbox Code Playgroud)