Opening a folder with GLFW? (LWJGL3)

Started by Lightbuffer, September 28, 2021, 12:02:06

Previous topic - Next topic

Lightbuffer

Hello,

How can I open a folder to the user through Java/GFLW? I can't use AWT's stuff due to the function that I used in LWGJL2 is not working with AWT's headless mode for some reason, and I couldn't find anything relevant in Google. I'm open to OS-specific implementations, but I need at least it to work on Windows.

Thank you for attention!  :)

spasi

Hey Lightbuffer,

LWJGL has bindings to Native File Dialog and tiny file dialogs. Try both and use the one that feels/works better for you.

Lightbuffer

Quote from: spasi on September 28, 2021, 13:20:06
Hey Lightbuffer,

LWJGL has bindings to Native File Dialog and tiny file dialogs. Try both and use the one that feels/works better for you.
Thank you for a quick response! This information would be helpful in the future, however I tested both and it's not exactly what I needed. Sorry for being vague, I suppose that's why I couldn't find the solution for my problem in the first place. What I meant by "opening a folder" is: I give a path to a folder, and it opens a Windows explorer window within that folder, kind of like command "explorer C:\Users\...\" in cmd.exe would do, i.e. I don't need to query for user to pick some folder or a file, but these bindings are certainly will help in the future!

jakethesnake

I've stolen this from somebody online. Can't remember who. It works with lwjgl 3, should probably work with 2, despite awt.

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.util.Locale;

final class FileOpener {

	public static void open(File file) {

		if (!openSystem(file.getPath()) && !openDESKTOP(file))
			System.err.println("unable to open file " + System.getProperty("os.name"));
	}

	private static boolean openSystem(String what) {

		String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);

		if (os.contains("win")) {
			return (run("explorer", "%s", what));
		}

		if (os.contains("mac")) {
			return (run("open", "%s", what));
		}

		return run("kde-open", "%s", what) || run("gnome-open", "%s", what) || run("xdg-open", "%s", what);

	}

	private static boolean openDESKTOP(File file) {

		if (Desktop.isDesktopSupported() && Desktop.getDesktop().isSupported(Desktop.Action.OPEN)) {
			try {
				Desktop.getDesktop().open(file);
				return true;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return false;

	}

	private static boolean run(String command, String arg, String file) {

		String[] args = arg.split(" ");
		String[] parts = new String[args.length + 1];
		parts[0] = command;
		for (int i = 0; i < args.length; i++) {
			parts[i + 1] = String.format(args[0], file).trim();
		}

		try {
			Process p = Runtime.getRuntime().exec(parts);
			if (p == null)
				return false;

			try {
				if (p.exitValue() == 0)
					return true;
				return false;
			} catch (IllegalThreadStateException itse) {
				return true;
			}
		} catch (IOException e) {
			//e.printStackTrace();
			return false;
		}
	}

}

Lightbuffer

Thank you very much jakethesnake, that is working!  ;D