Here you can find the source of chooseSaveFile(Component parent, File defaultFile, FileFilter filter)
Parameter | Description |
---|---|
parent | The parent window for the dialog. |
defaultFile | The default file to pre-select in the dialog, or null. |
filter | The filter that indicates what files can be viewed. |
public static File chooseSaveFile(Component parent, File defaultFile, FileFilter filter)
//package com.java2s; //License from project: Open Source License import java.awt.Component; import java.io.*; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; public class Main { /**/*w w w . j a v a 2 s.c o m*/ * This method shows a dialog that the user can use to select a file, which * it returns. If the user cancels, this method returns null. * * @param parent The parent window for the dialog. * @param defaultFile The default file to pre-select in the dialog, or null. * @param filter The filter that indicates what files can be viewed. * @return The file selected, or null if the user cancels. */ public static File chooseSaveFile(Component parent, File defaultFile, FileFilter filter) { JFileChooser chooser = new JFileChooser(); chooser.setFileFilter(filter); if (defaultFile != null) { chooser.setSelectedFile(defaultFile); } int result = chooser.showSaveDialog(parent); if (result == JFileChooser.APPROVE_OPTION) { return chooser.getSelectedFile(); } else { return null; } } }