java - 列出所有可以在java中打开特定文件类型的已安装程序

标签 java

是否可以从 Java 操作系统中获取可以打开某种文件类型的所有应用程序的列表?

例如,可以打开 .txt 或 .pdf 文件的所有应用程序的列表。

最佳答案

您应该查看此处的 JFace Eclipse 插件示例:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Showstheextensionsonthesystemandtheirassociatedprograms.htm

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.*;

/**
* This class shows the extensions on the system and their associated programs.
*/
public class ShowPrograms {



    /**
    * Runs the application
    */
    public void run() {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Show Programs");
        createContents(shell);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    /**
    * Creates the main window's contents
    * 
    * @param shell the main window
    */
    private void createContents(Shell shell) {
        shell.setLayout(new GridLayout(2, false));

        // Create the label and combo for the extensions
        new Label(shell, SWT.NONE).setText("Extension:");
        Combo extensionsCombo = new Combo(shell, SWT.BORDER | SWT.READ_ONLY);
        extensionsCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Create the label and the
        new Label(shell, SWT.NONE).setText("Program:");
        final Label programName = new Label(shell, SWT.NONE);
        programName.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Fill the combo with the extensions on the system
        String[] extensions = Program.getExtensions();
        for (int i = 0, n = extensions.length; i < n; i++) {
            extensionsCombo.add(extensions[i]);
        }

        // Add a handler to get the selected extension, look up the associated
        // program, and display the program's name
        extensionsCombo.addSelectionListener(new SelectionAdapter() {
            public void widgetSelected(SelectionEvent event) {
                Combo combo = (Combo) event.widget;

                // Get the program for the extension
                Program program = Program.findProgram(combo.getText());

                // Display the program's name
                programName.setText(program == null ? "(None)" : program.getName());
            }
        });

        // Create a list box to show all the programs on the system
        List allPrograms = new List(shell, SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL
            | SWT.V_SCROLL);
        GridData data = new GridData(GridData.FILL_BOTH);
        data.horizontalSpan = 2;
        allPrograms.setLayoutData(data);

        // Put all the known programs into the list box
        Program[] programs = Program.getPrograms();
        for (int i = 0, n = programs.length; i < n; i++) {
            String name = programs[i].getName();
            allPrograms.add(name);
            allPrograms.setData(name, programs[i]);
        }

        // Add a field for a data file
        new Label(shell, SWT.NONE).setText("Data File:");
        final Text dataFile = new Text(shell, SWT.BORDER);
        dataFile.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        // Double-clicking a program in the list launches the program
        allPrograms.addMouseListener(new MouseAdapter() {
            public void mouseDoubleClick(MouseEvent event) {
                List list = (List) event.widget;
                if (list.getSelectionCount() > 0) {
                    String programName = list.getSelection()[0];
                    Program program = (Program) list.getData(programName);
                    program.execute(dataFile.getText());
                }
            }
        });

        // Let them use launch instead of execute
        Button launch = new Button(shell, SWT.PUSH);
        data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalSpan = 2;
        launch.setLayoutData(data);
        launch.setText("Use Program.launch() instead of Program.execute()");
        launch.addSelectionListener(new SelectionAdapter() {
          public void widgetSelected(SelectionEvent event) {
              // Use launch
              Program.launch(dataFile.getText());
          }
        });


    }



    /**
    * The application entry point
    * 
    * @param args the command line arguments
    */
    public static void main(String[] args) {
        new ShowPrograms().run();
    }
}

关于java - 列出所有可以在java中打开特定文件类型的已安装程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23786460/

相关文章:

java - 检查是否已加载 dll 库? ( java )

java - 如何将 Java 库移植到 Web 上?

java - File.getClass().getMethod() : how to get . 类和方法

java - 在 RxJava 中,如何在链接 observables 时传递变量?

java - JDB 在每次异常时停止——如何防止这种情况

java - 为什么我的程序崩溃了(java)?

Java:VM 初始化期间发生错误

java - 从 Linked hashMap 派生时数组的顺序是什么?

java - 检查数组中的负数并重新提示输入正确

java - 对于带有 netty 3.10.6 的套接字客户端,异常 : java. nio.channels.NotYetConnectedException