2

I'm beginning to dabble with GUI frameworks and have hit a wall in the development of a data-managing GUI I'm creating for my father, using AWT.

I want to select a directory using FileDialog, in order to retrieve its PATH but for now I can only select files

There aren't concrete restrictions but for my sake:

  • It only uses AWT framework, no Swing
  • It's OS portable
  • It specifically selects directories, NOT files

I've searched for about 1.5 hours with mostly fruitless results, here's my attempt at my goal:

private static String fileDialog(Frame frame, String rootDir) {
    FileDialog filedialog = new FileDialog(frame, "Choose a File", FileDialog.LOAD);
    filedialog.setDirectory(rootDir);
    filedialog.setVisible(true);
    String dirName = filedialog.getDirectory();
    return dirName;
}

However I did discover a partial solution here, using:

System.setProperty(apple.awt.fileDialogForDirectories, “true”)

But, I want it to be OS portable, which this last line does not allow.

1
  • "It only uses AWT framework, no Swing" Why use AWT? See this answer for many good reasons to abandon AWT using components in favor of Swing. Dec 27, 2015 at 5:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.