So, i am showing a file chooser dialog using:
JFileChooser c = new JFileChooser();
int rVal = c.showSaveDialog(this);
if (rVal == JFileChooser.APPROVE_OPTION) {
saveFile((c.getCurrentDirectory().toString() + "/" + c.getSelectedFile().getName()), "data to be saved");
}
It works fine, but problem is user himself has to write a file name at the file prompt where file name is to be provided or he can select a file from file listing so that it can be overwritten.
I want to know is there any way that the file chooser text box already shows the users suggested file name so that he simply clicks on save and it works.
Just call setSelectedFile method before showing the dialog.
Related
I have a text editor program that opens up a file chooser whenever you click on Save in a Menu.
In order to achieve this saving process I create a file and save it with the current path in the file chooser.
// jfc = JFileChooser
String path = jfc.getSelectedFile().toString();
File testFile = new File(path);
This seems to work when I select an existing file in the file chooser, I.E "bio.txt" gets saved as a String in my program with the path "C:/Rikard/bio.txt"
But whenever I type in a new file name in the File name section and click Save, it doesn't save as a String and i get a NullPointerException, for Example Rikard.txt. It says in the method description that getSelectedFile() gets the file from user input by typing.
What could be wrong with my code?
I am trying to open a JFileChooser dialog to let the user decide his wish-directory for following operations.
Following is my current code:
JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
chooser = new JFileChooser(pref.get("LAST_PATH", ""));
} else{
chooser = new JFileChooser(home_dir);
}
//chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
int retVal = chooser.showOpenDialog(frame);
System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory().toString());
home_dir is a static String pointing to the users Download-directory.
The behaviour I do not understand:
home_dir = C:/Users/Scy/Downloads
Press OK without selecting any file(Or directory)
Output: C:/Users/Scy
home_dir = C:/Users/Scy/Downloads
Select any file within Downloads
Output: C:/Users/Scy/Downloads
Why do I not get the full-path(C:/Users/Scy/Downloads) as output when not selecting anything and just pressing OK? (With DIRECTORIES_ONLY activated, can't press OK without selecting anything without DIRECTORIES_ONLY)
Edit: I just noticed that when I just press the Cancel button without selecting anything the output is indeed what I expect, C:/Users/Scy/Downloads.
Based on an answer on this post I tried the following:
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(home_dir)); //home_dir = "C:/Users/Scy/Downloads"
The result was exactly the same as above. Pressing the Cancel button results in the full path output, while pressing OK/Accept results in C:/Users/Scy.
Maybe the 'selected file'(or directory) is in the 'current directory' (which you are retrieving atm.)?
If you want the current selected file, chooser.getSelectedFile() is what you are looking for. Keep in mind that when switching to DirectoryOnly mode this method will return a directory (e.g. a File instance representing a directory).
The method chooser.getCurrentDirectory() will return the parent directory of the current selected file which explains the unexpected results. (getSelectedFile.getParentFile() will most likely return the same file)
If you are trying to retrieve the parentDirectory, you set the starting directory incorrect. Notice how you pass in the first constructor a selected File? This means in the second constructor the 'home_dir' will be the selected File. If you only want to set 'home_dir' as starting directory, you should use the no-args constructor and call chooser.setCurrentDirector(new File(home_dir)) instead. Here is a snippit of what your code could look like:
JFileChooser chooser;
if(pref.get("LAST_PATH", "") != null){
// set last SELECTED file/directory path.
chooser = new JFileChooser(pref.get("LAST_PATH", ""));
} else{
// set currentDirectory, but dont select anything yet.
chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(home_dir));
}
getCurrentDirectory() returns a directory name, not a file name. If the user selects a file, this method returns the name of the directory which contains that file. If you want the name of the file which was selected, you should use getSelectedFile(). If you haven't yet, you should read this Oracle tutorial on file choosers.
I'm using JFileChooser with Swing to get the input files. In the file open window, people will copy and paste the url into the "File Name:" and hit enter, then you will see all the files under that url folder. It was all working, but when you include the http: in that url, the file open window would close when hit enter, without letting you choose the files under that url folder and the address is not returned by the showOpenDialog method correctly.
I don't know what "http:" in the url will do to the JFileChooser, but my goal is to let the JFileChooser act like there is no "http:" in the url and display the spreadsheet file under that url folder.
Below is the code I used for the JFileChooser:
fc = new JFileChooser();
FileFilter xlsxExcelType = new FileNameExtensionFilter("Excel spreadsheet (.xlsx)", "xlsx");
fc.addChoosableFileFilter(xlsExcelType);
fc.addChoosableFileFilter(xlsxExcelType);
fc.setFileFilter(xlsExcelType);
fc.setCurrentDirectory(new java.io.File("."));
int returnVal = fc.showOpenDialog(CampaignTestFrame.this);
I want to use the JFileChooser in Java to get the user to select where they would like to save a file. I have used this code:
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
This works and does get the path however the dialog box requires the user to select a file in order to get the file path. As i want it to save a new file i need it to get the path without having to select a file but from the folder the user has selected instead.
Im new to this and not sure of any other way of doing this, please could you advise me on a way around this.
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
After a bit of googling I'm not seeing this turn up much. Is there some "generic" way to forces the user to select a file that "already exists"
I could add something like
http://coding.derkeiler.com/Archive/Java/comp.lang.java.help/2004-01/0302.html or like JFileChooser with confirmation dialog but is there some canonical way?
Thanks.
First you need to check if your better option is choser.showOpenDialog or showSaveDialog
Save Dialog will let you select any name in the specified path it could be a non existent file, but open will always accept the selected file.. and you can safely add a file.exists() to ensure the file exists. You can also change the text of the buttons.. dialog.. etc..
JFileChooser chooser = new JFileChooser();
chooser.setApproveButtonText("Save");
int result = chooser.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
File selection = chooser.getSelectedFile();
//verify if file exists
if(selection.exists()){
//you can continue the code here or call the next method or just use !exists and behavior for wrong file
}else{
//System.exit(0), show alert.. etc
}
}
Sounds like you want an "Open" behavior, but a confirmation button that says "Save" instead of "Open".
You can do this via this method:
http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html#showDialog%28java.awt.Component,%20java.lang.String%29
Pass in "Save" for the approveButtonText argument.
As simple as this:
JFileChooser fc = new JFileChooser();
while(true)
{
if(fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION &&
!fc.getSelectedFile().exists())
JOptionPane.showMessageDialog(null, "You must select an existing file!");
else break;
}