问题
I want to select the folder that is selected.
JFileChooser targetDir = new JFileChooser();
targetDir.setDialogTitle("Choose Target Directory.");
targetDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if(targetDir.showOpenDialog(null)==JFileChooser.APPROVE_OPTION)
{
System.out.println(targetDir.getCurrentDirectory());
main_mw = new MainWindow("XYZ Copier");
main_mw.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
} else {
System.exit(0);
}
} else {
}
It gives the output "/home/rahul/Downloads/mc" but I need "/home/rahul/Downloads/mc/lib". It gives same result if i go inside lib.
Screenshots:
回答1:
JFileChooser#getSelectedFile will return the selected file/directory
getCurrentDirctory
returns the directory which is currently been shown in the chooser
来源:https://stackoverflow.com/questions/29422453/how-to-get-selected-folder-name-in-java-using-jfilechooser