Using Windows Explorer's "Open File" function in Java

10,469

Solution 1

Have a look at using JFileChooser while using the native system's look & feel:

enter image description here

public class NativeOpenDialogDemo {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                   ex.printStackTrace();
                }

                final JFrame frame = new JFrame("Open File Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                JButton openButton = new JButton("Open");
                openButton.addActionListener(new ActionListener() {
                    
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser chooser = new JFileChooser();
                        if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                            // do something
                        }
                    }
                });
                frame.add(openButton);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

Solution 2

We can use JFileChoose,

JFileChooser chooser = new JFileChooser();
            int status = chooser.showOpenDialog(null);
            if (status == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                if (file == null) {
                    return;
                }

                String fileName = chooser.getSelectedFile().getAbsolutePath();
   ......

            }
Share:
10,469

Related videos on Youtube

Jasmine Mercier
Author by

Jasmine Mercier

Updated on June 04, 2022

Comments

  • Jasmine Mercier
    Jasmine Mercier almost 2 years

    Using this in Java will open Windows Explorer to the C drive:

    Desktop.getDesktop().open(new File("c:\\"));
    

    However, I also need the "Open File" functions highlighted here: http://i.imgur.com/XfgnozF.jpg

    Is there a way to implement this in Java (using Windows Explorer, not Swing's FileChooser)?

    • Reimeus
      Reimeus almost 11 years
      That image is the open dialog from MS Word not Windows Explorer. The latter has no open dialog
    • Jasmine Mercier
      Jasmine Mercier almost 11 years
      Yea just as an example
    • Reimeus
      Reimeus almost 11 years
      You need to be more specific. Windows Explorer has no open dialog whereas there is an open dialog for all MS Office apps
    • MadProgrammer
      MadProgrammer almost 11 years
      @JasmineMercier The question is, why? What's wrong with JFileChooser? Have you tried FileDialog
    • camickr
      camickr almost 11 years
      Java doesn't have access to the API of Windows applications so you can't invoke those dialogs.
    • Jasmine Mercier
      Jasmine Mercier almost 11 years
      The program is for non-technical people and I'd like the look to match what they're used to(Windows Explorer, not Swing's FileChooser)
    • Reimeus
      Reimeus almost 11 years
      Then set the L&F to that of the current platform
    • Jasmine Mercier
      Jasmine Mercier almost 11 years
      That's what I was looking for. Thank you
    • MadProgrammer
      MadProgrammer almost 11 years
      @Reimeus I think you need to make that an answer, may be with a little demo code!
    • John Ranger
      John Ranger over 4 years
      The question is very ok. I don't understand the negative votes. Therefore +1 to compensate for the negative vote.
  • Jasmine Mercier
    Jasmine Mercier almost 11 years
    (using Windows Explorer, not Swing's FileChooser)
  • MadProgrammer
    MadProgrammer almost 11 years
    +1 for finding the problem and solution