Java – Using Windows Explorer’s “Open File” function in Java

file-iojavaswingwindowswindows-explorer

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)?

Best Solution

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) {
                }

                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);
            }
        });
    }
}