Here is the example from the JFileChooser
docs copy pasta with the parent sent to null
.
public class PickAFile {
public static void main(String[] args){
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}
}
}
If you don't like the look of the JFileChooser try the FileDialog
.
FileDialog dialog = new FileDialog((Frame)null, "Select File to Open");
dialog.setMode(FileDialog.LOAD);
dialog.setVisible(true);
String file = dialog.getFile();
dialog.dispose();
System.out.println(file + " chosen.");
** The call to dispose is necessary to exit the program if this is an isolated call, or to prevent a memory leak if this is used in a larger application.