How to use FileDialog?

grb picture grb · Aug 27, 2011 · Viewed 48.4k times · Source

I created an interface and I'd like to add a function that allows user to open a file. I'm using AWT. I don't understand how to use FileDialog. Can you please give me an example or a good link that explain this?

Answer

Salvatorelab picture Salvatorelab · Mar 12, 2013

A complete code example, with file filtering:

FileDialog fd = new FileDialog(yourJFrame, "Choose a file", FileDialog.LOAD);
fd.setDirectory("C:\\");
fd.setFile("*.xml");
fd.setVisible(true);
String filename = fd.getFile();
if (filename == null)
  System.out.println("You cancelled the choice");
else
  System.out.println("You chose " + filename);