user7950306
user7950306

Reputation:

How do you open a system file explorer in java to select a file for saving or loading?

I am trying to create a simple pixel art drawing application in java. Right now to load and save a file you have to use the console and input the path. I'm wondering how would I open a file explorer and allow the user to select a file through it. I have used a JFileChooser in the past but I'm asking about how to open the OS file explorer. Thanks!

EDIT: I guess I was confusing the first time. I don't want to just open a file explorer I want to be able to have my program interact with it and allow the user to select a file with it. Like this: Explorer being used in notepad++ to open a file

EDIT: So I found FileDialog which seems to do what I want except that it is designed for AWT and I am using OpenGL. I can try and make that work but if andyone has anything that works with lwjgl that would be greatly appreciated.

Upvotes: 2

Views: 6699

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

For a cross platform solution, you could use the Desktop API, see How to Integrate with the Desktop Class for more details

Maybe something like...

if (Desktop.isDesktopSupported()) {
    Desktop desktop = Desktop.getDesktop();
    desktop.browse(uri); // Throws
}

When running on Windows, I tend to use something like...

String path = file.getCanonicalPath();

ProcessBuilder pb = new ProcessBuilder("explorer.exe", "/select," + path);
pb.redirectError();
Process proc = pb.start();

which will highlight the specified file, but is only suitable when running on Windows

I tend to switch between the two based on the platform the app is running on

Upvotes: 2

Related Questions