Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using FileChooser to save a file with default filename

Tags:

javafx-2

I wat to save a file.I use this.

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

But in the dialog I want to suggest a name for the file, so that the user only selects a directory for the given file.The name of the file is known already.So i want to suggest that filename.

ThankYou.

like image 924
aaaa Avatar asked Mar 09 '13 11:03

aaaa


2 Answers

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

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

Now, I don't think there is anyway to STOP the user from choosing a different file, but at leas this will give them a default you want them to pick.

like image 150
WillBD Avatar answered Sep 27 '22 18:09

WillBD


Initial file name providing - it is a thing, which requires to transfer your string (initial name) through native call, to the call of the native file chooser. It is a complex thing, and you can look at these issues about its implementing :

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

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

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

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

So, you can specify initial file name for a file, starting from JDK 8 (you can access it, downloading JDK early access).

Recently, I've tested this feature, and it is working.

There is a method setInitialName() or smth like that.

And, as I've mentioned, it is a complex thing, and you are not likely to be able to implement it by yourself (until you are able to build jfx).

So, the decision - to wait until JDK8 release, or to use early access builds. Or, to use your own implementation of file chooser.

like image 24
Alexander Kirov Avatar answered Sep 27 '22 18:09

Alexander Kirov