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:
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();
......
}
Related videos on Youtube
Author by
Jasmine Mercier
Updated on June 04, 2022Comments
-
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 almost 11 yearsThat image is the open dialog from MS Word not Windows Explorer. The latter has no open dialog
-
Jasmine Mercier almost 11 yearsYea just as an example
-
Reimeus almost 11 yearsYou need to be more specific. Windows Explorer has no open dialog whereas there is an open dialog for all MS Office apps
-
MadProgrammer almost 11 years@JasmineMercier The question is, why? What's wrong with
JFileChooser
? Have you triedFileDialog
-
camickr almost 11 yearsJava doesn't have access to the API of Windows applications so you can't invoke those dialogs.
-
Jasmine Mercier almost 11 yearsThe 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 almost 11 yearsThen set the L&F to that of the current platform
-
Jasmine Mercier almost 11 yearsThat's what I was looking for. Thank you
-
MadProgrammer almost 11 years@Reimeus I think you need to make that an answer, may be with a little demo code!
-
John Ranger over 4 yearsThe question is very ok. I don't understand the negative votes. Therefore +1 to compensate for the negative vote.
-
-
Jasmine Mercier almost 11 years(using Windows Explorer, not Swing's FileChooser)
-
MadProgrammer almost 11 years+1 for finding the problem and solution