Pregunta

The following code works fine when returning a file on a mac since it automatically appends the file extension to the name of the file.

On windows however i have to type in the extension of the file as part of the file name in order for it to return with that extension....even though the extension is selected in the 'save type as' pulldown menu.

is there a way to automatically append the extension to the name when returning a file from the filechooser on windows?

FileChooser.ExtensionFilter extFilter = new   FileChooser.ExtensionFilter(fileExtension.toUpperCase()+" files(*."+fileExtension+")", "*."+fileExtension);
                fileChooser.getExtensionFilters().add(extFilter);

                //Show save file dialog 
                final File file = fileChooser.showSaveDialog(MyStage.this);
¿Fue útil?

Solución

I ran into the same issue. My solution was to make a new File, and append the file extension as a string in the File constructor.

If you want users to be able to select and overwrite an existing file, make sure and add a check to make sure the initial save file does not contain the particular extension already before appending or else you will get something like "test.xls.xls".

FileChooser fc = new FileChooser();
    FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("XLS File (*.xls)", "*.xls");

    fc.getExtensionFilters().add(extFilter);

    File save  = fc.showSaveDialog(stage);

    save = new File(save.getAbsolutePath()+".xls");

    FileOutputStream fileOut = new FileOutputStream(save);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top