Search code examples
javafilejavafxfilechooser

JavaFX FileChooser new file


Is it possible to use the JavaFX File Chooser (or a similar alternative) to create new files?

Entering the name of a non-existent file works on Linux (Ubuntu to be exact) but on Windows the file chooser does not allow that.


Solution

  • Yes that should be possible, you just need to know the right function to call. The API for FileChooser details them in its opening paragraph here.

    A FileChooser can be used to invoke file open dialogs for selecting single file (showOpenDialog), file open dialogs for selecting multiple files (showOpenMultipleDialog) and file save dialogs (showSaveDialog).

    USAGE

    Save file:

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

    Open one file:

        FileChooser fileChooser = new FileChooser();
        File selectedFile = fileChooser.showOpenDialog(null);
    

    Open multiple files:

        FileChooser fileChooser = new FileChooser();
        List<File> files = fileChooser.showOpenMultipleDialog(null);