Java JFileChooser chooseSaveFile(Component parent, File defaultFile, FileFilter filter)

Here you can find the source of chooseSaveFile(Component parent, File defaultFile, FileFilter filter)

Description

This method shows a dialog that the user can use to select a file, which it returns.

License

Open Source License

Parameter

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.

Return

The file selected, or null if the user cancels.

Declaration

public static File chooseSaveFile(Component parent, File defaultFile, FileFilter filter) 

Method Source Code


//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;
        }
    }
}

Related

  1. chooseFiles(String title, File currentDir)
  2. chooseFilesForOpen(String title, FileFilter filter, File initialDir)
  3. chooseFileToRead(Component owner, FileFilter filter, File pathToNavigateTo)
  4. chooseNewFile(final Component parent, final File startingDirectory)
  5. chooseOpenFile(String dir, String fileName, String fileNameExtension, Component parent)
  6. chooseSaveFile(String dir, String initialFile, String fileNameExtension, Component parent)
  7. choseFile(File selectedFile, String startDir, final String description, final String extension, Component parent, boolean addExtension, boolean forLoad)
  8. cleanFileSelector(JFileChooser filterUpdates)
  9. cleanFileSelector(JFileChooser filterUpdates)