简体   繁体   中英

How to load a File using JFileChooser?

In Java I want to load a file [whatever format it is] in its own format using JFileChooser . Means I don't want to read and display the contents inside my JFrame . Instead I want them to open/load like an image open in a Windows Photo Viewer/Irfan Viewer and a PDF open in Adobe Reader By clicking a Button.

I had searched a lot. But all tutorial I read tells how to print a line "opening this file/You are selected this file" by clicking a JButton . Nobody is actually opening/loading a file on a button click. May be I am not getting correctly what they said because I am new to Java. I hope my problem is clear and please help...

Here is the code that I got from a tutorial page:

 public class JFileChooserTest {

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JDialog.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("JComboBox Test");
        frame.setLayout(new FlowLayout());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JButton button = new JButton("Select File");
        button.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent ae) {
            JFileChooser fileChooser = new JFileChooser();
            int returnValue = fileChooser.showOpenDialog(null);
            if (returnValue == JFileChooser.APPROVE_OPTION) {
              File selectedFile = fileChooser.getSelectedFile();
              System.out.println(selectedFile.getName());
            }
          }
        });
        frame.add(button);
        frame.pack();
        frame.setVisible(true);
    }    
}

Here is what I want to do with Java. here is an example with windows:

A Browse Button Click opens this window

And when I select the XLS file and click OPEN button, an XLS file will open. I want to do the exact same with Java. Hope it is more clear now.

You may try using Desktop.open() :

Desktop.getDesktop().open(selectedFile);

EDIT You need to update here:

button.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent ae) {
      JFileChooser fileChooser = new JFileChooser();
      int returnValue = fileChooser.showOpenDialog(null);
      if (returnValue == JFileChooser.APPROVE_OPTION) {
        File selectedFile = fileChooser.getSelectedFile();
        java.awt.Desktop.getDesktop().open(selectedFile);//<-- here
      }
   }
});

Sample code from site :

If I understand you correctly, you want to select a file and pass it to the system's default application. Unfortunately, this is highly dependable on your operating system. For Windows you can pass that to the command line like this:

        String systemcall = "cmd /C start \"\" \"" + absolutePath + "\"";
        Runtime runTime = Runtime.getRuntime();
        HomeLogger.instance().info("EXECUTE " + systemcall);
        runTime.exec(systemcall);

The String absolute Path must be the exact locatin of the file, eg "C:\\test.txt". I hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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