Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get full path directory from File Chooser

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.

What the file chooser looks like

When the user clicks open here, I want it to return the full path and not the file. How do I do this?

like image 975
newSpringer Avatar asked May 16 '12 15:05

newSpringer


People also ask

How do I find my JFileChooser file name?

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()).

How do I use file chooser?

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.

How do you create a file path?

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.


2 Answers

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

like image 112
Malcolm Smith Avatar answered Sep 19 '22 14:09

Malcolm Smith


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.

like image 39
Guillaume Polet Avatar answered Sep 19 '22 14:09

Guillaume Polet