Get the path of chosen file java
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);
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();
}
File f = chooser.getSelectedFile();
String absPath = f.getAbsolutePath();
File f = chooser.getSelectedFile();
String path = f.getAbsolutePath();
精彩评论