Trying to use JFileChooser to select a folder - java

I'm trying to use JFileChooser to select a folder. It returns the parent folder, not the current folder.
Example I select folder documents, then backup. JFileChooser returns documents instad of backup.
String getFilePath()
{
JFileChooser fc = new JFileChooser();
// fc.setCurrentDirectory(new File(System.getProperty("user.home")));
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File rt=fc.getCurrentDirectory();
String t=rt.getName();
return t;
}
return null;
}

The issue is that you are using JFileChooser.getCurrentDirectory() instead of JFileChooser.getSelectedFile().

Related

Is there a workaround for: FileFilter doesn't work in Windows?

The following JFileChooser code works fine, except that the FileFilter doesn't filter. It doesn't do anything. From another stackoverflow answer: "Filename filters do not function in Sun's reference implementation for Microsoft Windows."
Comment from Nov 21st, 2016
Is there a FileFilter workaround for Windows?
public String getPathFileName(String startingDir) {
String returnSelectedFile = "";
JFileChooser fileChooser = new JFileChooser(startingDir);
FileFilter filter = new FileNameExtensionFilter("Excel file", "xls", "xlsx");
fileChooser.addChoosableFileFilter(filter);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
returnSelectedFile = selectedFile.getPath();
}
return returnSelectedFile;
}
I have found this to work:
final JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("CSV FILES", "csv"));
I have found that this works for one file filter, but I cannot confirm for multiple file filters. Hope this helps.

How to get the file extension in JfileChooser if the user input the filename without extension

I used the JFileChooser in my app. My question is if the user typing file name without the file extension on the dialog box; the app will allow more another file extension. How can I get the file extension on the file as type drop down box. I used fileChooser.getSelectedFile().getName() or fileChooser.getSelectedFile().getAbsolutePath() didn't get the extension. Would someone help me how to do it.
There is my code:
#SuppressWarnings("static-access")
private String getSavedFileName()
{
String rtnValue="";
JFileChooser fileChooser = new JFileChooser();
String PDFEXTENSION= "pdf";
FileNameExtensionFilter pdfType=new FileNameExtensionFilter("PDF File (."+ PDFEXTENSION + ")", PDFEXTENSION);
fileChooser.addChoosableFileFilter(pdfType);
fileChooser.setFileFilter(pdfType);
//clear "All files" from dropdown filter box
fileChooser.setAcceptAllFileFilterUsed(false);
int returnVal = fileChooser.showSaveDialog(this.splitRightPane);
if (returnVal == fileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
// String extension = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());
String fileAbsolutePath=file.getName();
file.getAbsolutePath()
String extension =fileAbsolutePath.substring(fileAbsolutePath.lastIndexOf(".") + 1, fileAbsolutePath.length() );
if ( PDFEXTENSION.equalsIgnoreCase(extension)){
rtnValue=fileAbsolutePath;
} else{
Utility.DisplayWarningMsg("Only PDF File");
}
} else if (returnVal == JFileChooser.CANCEL_OPTION ) {
// Do something else
}
return rtnValue;
}

How to set the last path as the next default using JFileChooser

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 to prompt user for file name?

export content to an xls file using java with save dialog:
- Where should I ask for a file name during save?
I prefer to ask what to name the file when it is saved.
public static void main(String args[])
{
SaveToExcel exp = new SaveToExcel();
exp.writePropertiesIntoExcel("D:\\ExcelFile.xls");
}
You should use JFileChooser if you want to let the users choose where to save the file.
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
chooser.setMultiSelectionEnabled(false);
int returnVal = chooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
File f = chooser.getSelectedFile();
String a = f.getAbsolutePath();
SaveToExcel exp = new SaveToExcel();
exp.writePropertiesIntoExcel(a);
}
A simple code you can use in your method, this gets the path you want to save your file in.

JFileChooser selected direcory

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

Categories