Using FileChooser to save a file with the default file name

I want to save a file. I am using this.

FileChooser fileChooser = new FileChooser(); File file = fileChooser.showSaveDialog(null); 

But in the dialog box I want to offer a file name, so that the user selects only the directory for this file. The file name is already known. Therefore, I want to suggest this file name.

ThankYou.

+4
source share
3 answers

This is now fixed in Javafx 2.2.45 (bundled with java 7.0_45), and you can do what the OP offers with the following fileChooser property, setInitialFilename, which is used as such:

  FileChooser myFile = new FileChooser(); myFile.setInitialFileName("Whatever_file_I_want.coolFile"); 

Now I don’t think that in any case, stop the user from choosing another file, but in leasing this will give them the default one that you want to select.

+6
source

The original file name is a thing that requires passing your string (initial name) through your own call, by calling your own file selection. This is a complicated thing, and you can look at these questions about its implementation:

http://javafx-jira.kenai.com/browse/RT-16111 (primary)

http://javafx-jira.kenai.com/browse/RT-24588

http://javafx-jira.kenai.com/browse/RT-24612

They all have a fixed version of lombard, so they are fixed in JDK 8.

So, you can specify the initial file name for the file starting with JDK 8 (you can access it by downloading early JDK access).

I recently tested this feature and it works.

There is a setInitialName () or smth method.

And, as I mentioned, this is a complicated thing, and you are unlikely to be able to implement it yourself (until you can build jfx).

So, the solution is to wait for the release of JDK8 or use early access builds. Or, to use your own file selection implementation.

+3
source

Here is a workaround that worked for me:

you can use javafx.stage.DirectoryChooser to select the directory for the file you want to save, and after saving, create a new file in that directory with the default name and extension.

 DirectoryChooser dc = new DirectoryChooser(); File file = dc.showDialog(null); if (file != null) { file = new File(file.getAbsolutePath() + "/dafaultFilename.extension");} 
0
source

All Articles