繁体   English   中英

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

[英]List all the installed programs that can open a specific file type in java

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

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

您应该查看以下JFace Eclipse插件的示例: http//www.java2s.com/Code/Java/SWT-JFace-Eclipse/Showstheextensionsonthesystemandthethessssiatedprograms.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();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM