C graphics C graphics - 1 month ago 12
Java Question

java: Open folder on button click

In java, how can we open a separate folder (e.g. c:) for user on click of a button, e.g like the way " locate this file on disk" or "open containing folder" does when we download a file and we want to know where it was saved. The goal is to save user's time to open a browser and locate the file on disk.
Thanks ( image below is an example from what firefox does)
enter image description here

I got the answer:
Here is what worked for me in Windows 7:

File foler = new File("C:\\"); // path to the directory to be opened
Desktop desktop = null;
if (Desktop.isDesktopSupported()) {
desktop = Desktop.getDesktop();
}

try {
desktop.open(foler);
} catch (IOException e) {
}


Thanks to @AlexS

Answer

I assume you have a file. With java.awt.Desktop you can use something like this:

File myfile = new File("/tmp/test.txt");
String path = myFile.getAbsolutePath();
File dir = new File(path);
if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().open(dir);
}

But I don't know on which platforms this is supported.