JavaFX FileChooser does not return file extension for Windows

The following code works fine when returning a file to Mac, as it automatically adds the file extension to the file name.

On Windows, however, I have to enter the file extension as part of the file name so that it returns with this extension ... even if the extension is selected from the Save As drop-down menu.

Is there a way to automatically add the extension to the name when returning a file from a file in windows file?

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); 
+4
source share
1 answer

I ran into the same problem. My solution was to create a new file and add 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 you add a check to make sure that the original save file does not contain a specific extension before adding, otherwise 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); 
+2
source

Source: https://habr.com/ru/post/1435399/


All Articles