I was wondering how could a code using JFrame return a chosen path of a file.I don’t need a complex GUI or anything else, just something that allows me to pick a file and return the path i have chosen, stored a String variable. I’d like to know the easiest version of this code possible to be executed as well.
>Solution :
It is very easy to do using a JFileChooser (another built-in swing component).
Here is an example from the api documentation.
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this file: " +
chooser.getSelectedFile().getName());
}