I am creating an application using Netbeans 7.1.2 and I am using a file chooser, but i do not want the file chooser to get a file, instead i want it to return the full path to the directory that it is currently at.
When the user clicks open here, I want it to return the full path and not the file. How do I do this?
JFileChooser has a method, getSelectedFile(). Which is a File. If you open the dialog with showSaveDialog() you should be able to get the File from that (file. getName()).
Click the Open a File button. Navigate around the file chooser, choose a file, and click the dialog's Open button. Use the Save a File button to bring up a save dialog. Try to use all of the controls on the file chooser.
If you're using Windows 11, simply right-click on it. Then, select “Copy as path” in the contextual menu. Alternatively, in Windows 10, you can also select the item (file, folder, library) and click or tap on the “Copy as path” button from File Explorer's Home tab in Windows 10.
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("choosertitle");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
} else {
System.out.println("No Selection ");
}
From http://www.java2s.com/Code/Java/Swing-JFC/SelectadirectorywithaJFileChooser.htm
If you want to know the current directory:
fileChooser.getCurrentDirectory()
If you want to get the selected file:
fileChooser.getSelectedFile();
To get the absolute path to a file:
file.getAbsolutePath();
Grab all the infos on the File chooser API here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With