Search code examples
javafiledialog

Setting File Name in FileDialog


Is it possible set the text of the "Save As: " line in a FileDialog? I would like to set it so that when the dialog is created, there is a suggested name for the file instead of just blank space (as shown below). Thanks.

enter image description here


Solution

  • you can do that like .

     JFileChooser chooser = new JFileChooser();
     if (targetFile != null) {
     chooser.setSelectedFile(targetFile);
     } else {
     chooser.setSelectedFile(newFile("default.xyz"));
     }
     int option = chooser.showSaveDialog(null);
     if (option == JFileChooser.APPROVE_OPTION) {
     targetFile = chooser.getSelectedFile();
     }
    

    That pops up a Save file dialog in the working directory and dispalys default.xyz