Reputation: 25038
How to display a JFileChooser so I get the absolute path of a file, then assign the path to a string
I was doing something like:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
Upvotes: 0
Views: 7242
Reputation: 3324
File f = chooser.getSelectedFile();
String path = f.getAbsolutePath();
Upvotes: 0
Reputation: 2725
You need to complete like:
int option = chooser.showOpenDialog(parentComponent); // parentComponent must a component like JFrame, JDialog...
if (option == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
String path = selectedFile.getAbsolutePath();
}
Upvotes: 2
Reputation: 86754
File f = chooser.getSelectedFile();
String absPath = f.getAbsolutePath();
Upvotes: 2