I have next code:
public void actionPerformed(ActionEvent e) {
if (e.getSource() == btnNajitPDFCache) {
JFileChooser chooser;
String choosertitle = "Select directory.";
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setApproveButtonText("OK");
//
// disable the "All files" option.
//
chooser.setAcceptAllFileFilterUsed(false);
//
if (chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
textFieldPDFCache.setText(chooser.getCurrentDirectory()+"");
}
}
}
That's Ok. I choose c:\test folder in opened chooser form and next I click on button OK.
But chooser.getCurrentDirectory() return only c:\ . Why? What is wrong?
getCurrentDirectory() returns the current directory that is opened in the
JFileChooser. When you are selecting C:\test you opened C:\ directory, so you are getting C:\ on getCurrentDirectory()
The getSelectedFile() returns the file that is selected (in your case the file is a directory). So you if you want the directory that is selected by the user use getSelectedFile()
You should use chooser.getSelectedFile() instead.
You Can Set Canonical path as
File Canonicalpath = new File(new File("C:/").getCanonicalPath());
Related
How can I get the absolute path of a directory using JFileChooser, just selecting the directory?
Use:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//or
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
together with:
chooser.getCurrentDirectory()
//or
chooser.getSelectedFile();
then call getAbsoluteFile() on the File object returned.
JFileChooser's getSelectedFile() method, returns a File object.
Use the getAbsolutePath() to get the absolute name to the file.
modified example from the javadoc:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this directory: " +
chooser.getSelectedFile().getAbsolutePath());
}
Try:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file = chooser.getSelectedFile();
String fullPath = file.getAbsolutePath();
System.out.println(fullPath);
fullPath gives you the required Absolute path of the Selected directory
I use the following code I found on internet to select a folder:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Select destination folder");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
jTextField2.setText(chooser.getCurrentDirectory().getAbsolutePath());
} else {
System.out.println("No Selection ");
}
However if for example I browse to
"C:\testfolder\"
then the
"chooser.getCurrentDirectory().getAbsolutePath()"
returns
c:\
How can I resolve this to return "C:\testfolder\" ?
Use chooser.getSelectedFile() instead of chooser. getCurrentDirectory(). You might want have a look at How to Use File Choosers for more details.
You not asking the dialog for the currently selected file, but where the dialog was set to start from
I need to use
fileChooser.getSelectedFile()
method however it always returns language modified path because some directories are translated in osX. For example folder "/Downloads" is translated to my system language "/Stiahnuté" but real path is "/Downloads"
return:
/Users/John/Stiahnuté
expectation
/Users/John/Downloads
If I select some sub-directory then fileChooser.getSelectedFile() returns right path again. It looks that always only last directory in path is translated
/Users/John/Downloads/subDirectory
Code:
saveButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FolderFilter());
fileChooser
.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println("save path: "
+ selectedFile.getPath());
doSomething(selectedFile);
}
}
});
UPDATE:
I made little workaround but it is not perfect solution. However it works for me.
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"Directories", "dir");
fileChooser.setFileFilter(filter);
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
File newDir = new File(selectedFile.getPath());
if (!newDir.exists()) {
newDir.mkdir();
}
doSomething();
}
I can reproduce the problem on Mac OS X 10.11.4 with Java 1.8.0_66. For me this looks like a bug (or at least unexpected behavior) in the implementation of JFileChooser. You may open a bug report for the issue.
With the help of an answer explaining to use FileDialog to get a operating system native file chooser and another answer about using it to select directories I found the following workaround:
final Frame parent = …; // can be null
System.setProperty("apple.awt.fileDialogForDirectories", "true");
final FileDialog fileDialog = new FileDialog(parent);
fileDialog.setVisible(true);
System.setProperty("apple.awt.fileDialogForDirectories", "false");
final File selectedDirectory = new File(fileDialog.getDirectory(), fileDialog.getFile());
System.out.println(selectedDirectory);
System.out.println(selectedDirectory.exists());
Note that using "apple.awt.fileDialogForDirectories" is, of course, platform specific and will not work on other operating systems.
I have several dialog boxes which provide a File Chooser. For the first, my coding was like this
JFileChooser chooser= new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal= chooser.showOpenDialog(this);
if(returnVal==JFileChooser.APPROVE_OPTION){
File f= chooser.getSelectedFile();
jTextField1.setText(f.getPath());
chooser.setCurrentDirectory(f);
}
In my case, i would like to set the last path which is selected as the default path in next selection JFileChooser. Is there any solution for me?
Thanks for any response
Depending on your requirements, you can use Preferences to store it away and use it again after the program has been restarted.
Preferences pref = Preferences.userRoot();
// Retrieve the selected path or use
// an empty string if no path has
// previously been selected
String path = pref.get("DEFAULT_PATH", "");
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// Set the path that was saved in preferences
chooser.setCurrentDirectory(new File(path));
int returnVal = chooser.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File f = chooser.getSelectedFile();
chooser.setCurrentDirectory(f);
// Save the selected path
pref.put("DEFAULT_PATH", f.getAbsolutePath());
}
You will have to "remember" the last path.
This can easily be done by storing the value in a instance variable...
private File lastPath;
//...
lastPath = f.getParentFile();
And simply resetting it when you need to...
//...
if (lastPath != null) {
chooser.setCurrentDirectory(lastPath);
}
You could also use a single instance of the JFileChooser, so each time you show it, it will be at the last location it was used...
How can I get the absolute path of a directory using JFileChooser, just selecting the directory?
Use:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
//or
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
together with:
chooser.getCurrentDirectory()
//or
chooser.getSelectedFile();
then call getAbsoluteFile() on the File object returned.
JFileChooser's getSelectedFile() method, returns a File object.
Use the getAbsolutePath() to get the absolute name to the file.
modified example from the javadoc:
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this directory: " +
chooser.getSelectedFile().getAbsolutePath());
}
Try:
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
File file = chooser.getSelectedFile();
String fullPath = file.getAbsolutePath();
System.out.println(fullPath);
fullPath gives you the required Absolute path of the Selected directory