JFileChooser.showSaveDialog(…) - how to set suggested file name

前端 未结 3 1100
后悔当初
后悔当初 2020-11-30 04:43

The JFileChooser seems to be missing afeature: a way to suggest the file name when saving a file (the thing that usually gets selected so that it would get repl

3条回答
  •  [愿得一人]
    2020-11-30 05:04

    If I understand you correctly, you need to use the setSelectedFile method.

    JFileChooser jFileChooser = new JFileChooser();
    jFileChooser.setSelectedFile(new File("fileToSave.txt"));
    jFileChooser.showSaveDialog(parent);
    

    The file doesn't need to exist.

    EDIT: If you pass a File with an absolute path, JFileChooser will try to position itself in that directory (if it exists).

提交回复
热议问题