JFileChooser.showSaveDialog(…) - preserve suggested file name after changing directory - java

There are already some questions about how to set a default file name for a JFileChooser control.
I'm having a few problems with preserving that default filename when switching directories.
Right now, when I do that, the original filename I supplied get over overwritten by the path of the new directory itself.
Is there anything can be done in order to avoid this behavior?

You could add a PropertyListener to the file chooser, and if you get a "directoryChanged" property, set your default file again.
For example:
JFileChooser chooser = new JFileChooser();
chooser.addPropertyChangeListener( new PropertyChangeListener() {
public void propertyChange( PropertyChangeEvent evt )
{
if ( evt.getPropertyName().equals( "directoryChanged" ) )
{
JFileChooser me = (JFileChooser)evt.getSource();
me.setSelectedFile( new File( "text.txt" ) );
}
}
});
It seems like it might do what you want, but is more a workaround than a proper solution.

Related

File I/O Confusion

First question here on StackOverflow, so apologies if this is bad. But I got a student job programming for civil engineers. My first assignment is to use JFileChooser to allow the user to specify a desired file, and then the full path of this file will be written to a txt file. I want it to automatically write to the file that this program using JFileChooser resides in. I am very confused on how to do this and haven't been able to find anything helpful on that.
My code:
public class FilePathFinder {
JFileChooser fileChooser;
String path;
public static void main(String[] args) throws IOException{
String path = null; //String that will be outputted to
//creates file chooser and its properties
JFileChooser file_chooser = new JFileChooser();
file_chooser.setCurrentDirectory(new java.io.File("user.home"));
file_chooser.setDialogTitle("Create File Path");
file_chooser.setApproveButtonText("Create Path");
file_chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
file_chooser.setAcceptAllFileFilterUsed(false);
if (file_chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
path=(file_chooser.getSelectedFile().getAbsolutePath());
}
//Writes path name to file
String user_home_folder = System.getProperty("user.home");
System.out.println(user_home_folder);
File path_file = new File(user_home_folder, path);
BufferedWriter path_writer = new BufferedWriter(new FileWriter(path_file));
if(!path_file.exists()){
path_writer.write(path);
}
}
}
So what is the problem you are actually having?
A comment:
file_chooser.setCurrentDirectory(new java.io.File("user.home"));
This won't set the current directory to be the users home directory. But to a directory (if it exists) named "user.home" in the current directory. What you probably wanted to do is:
file_chooser.setCurrentDirectory(new java.io.File(System.getProperty("user.home")));
Update By reading your comment on this answer:
You already have an absolute path in your variable path. But using the constructor new File(user_home_folder, path) your prefixing it with the location of the user's home directory. This results in a path like that has for example the drive letter twice in it. Remove the first parameter of this constructor.

fileChooser.setDialogType() Doesn't Work

I have code that is supposed to choose a folder from a JFileChooser, but it still is acting like its choosing a file, even if I use fs.setDialogType. I tried showSaveDialog and showOpenDialog, but they both don't work.
Here is my code:
public static String getFolder() {
JFileChooser fs = new JFileChooser();
fs.setDialogType(JFileChooser.DIRECTORIES_ONLY);
fs.showSaveDialog(null);
if (fs.getSelectedFile() != null)
return fs.getSelectedFile().getAbsolutePath();
return "null";
}
setDialogType is used to set the open, save or custom type for the dialog. Use setFileSelectionMode to specify the whether the dialog should select files or directories
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

JFileChooser - open in current directory

I have a simple JFileChooser set up in the following manner
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File("."));
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.setFileFilter(new FileFilter() {
...
});
int v = chooser.showOpenDialog(this);
if (v == JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
System.out.println(file.getAbsolutePath());
}
As you can see, this FileChooser starts out in the current directory, which in my Netbeans project, is the root of the project folder. Here's the problem: When I select a file and it prints out the absolute path, it includes the "." in the path. For instance, the output I get is:
/Users/MyName/Folder1/Folder2/./Temp.xls
Of course, this is weird, especially since I'm displaying this to the user. Now, I could be hacky and do some fun post substring processing stuff to get rid of that "/./" portion. But...is there a non-lazy programmer way to fix this problem? Thanks in advance!
Use the system property "user.dir" as follows:
File workingDirectory = new File(System.getProperty("user.dir"));
chooser.setCurrentDirectory(workingDirectory);

Get all file names in directory using JFileChooser?

I'm using this bit of code:
fileBrowser() {
String toReturn = null;
JFileChooser Chooser = new JFileChooser();
int choosen = Chooser.showOpenDialog(fileBrowser.this);
if (choosen == JFileChooser.APPROVE_OPTION) {
System.out.println(Chooser.getCurrentDirectory().toString()+"\\"+Chooser.getSelectedFile().getName());
}
}
To get the selected file name and location, which is all working fine. I was wondering as an addition, is there also a way to get all the filenames in that directory as well? something like .getAllFiles() I've had a search around and can't find one?
Thanks in Advance.
Sure, use
File[] filesInDirectory = chooser.getCurrentDirectory().listFiles();
Then you can iterate over that array:
for ( File file : filesInDirectory ) {
System.out.println(file.getName());
}
Well, there's File.list(). This will list all files by their name from the specified directory (i.e. File). But this will also return directory names. In order to circumvent that, use the other File.list(FilenameFilter filter) method that will enable you to filter out directories from the listing.

set the JFileChooser to open current directory

I created a JFileChooser to open a file, but when I select a file and open it,for second the time that i want to select a file, the JFileChooser is not in the current directory.
How set the JFileChooser to open the current directory?
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES );
fileChooser.setDialogType(JFileChooser.SAVE_DIALOG);
int result = fileChooser.showOpenDialog( this );
if ( result == JFileChooser.APPROVE_OPTION ){
File fileName = fileChooser.getSelectedFile();
File path=fileChooser.getCurrentDirectory();
if ( ( fileName == null ) || ( fileName.getName().equals( "" ) ) )
{
JOptionPane.showMessageDialog( this, "Invalid File Name",
"Invalid File Name", JOptionPane.ERROR_MESSAGE );
}
else{
currentPath=path.getPath()+"\\"+fileName.getName();}
}
Either pass the directory into the constructor via the File parameter (a File can also be a directory, FYI), or use the .setCurrentDirectory(File dir) method before you make the JFileChooser visible.
Also, to make the JFileChooser stay in the same folder, you need to save the folder of the file/directory chosen from last time, and use THAT value to control which folder it starts in the subsequent times via .setCurrentDirectory(File dir)
Make the chooser a class level attribute and create it only once. That way, it not only points to where it did when closed, but will have the same size, location, file filter selected etc.

Categories

Resources