Java JFileChooser getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)

Here you can find the source of getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)

Description

get Save As File

License

Apache License

Declaration

public static File getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.swing.*;

import java.io.*;

public class Main {
    protected static String gLastDirectory = System.getProperty("user.dir");

    public static File getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension) {
        String name = defaultName;
        if (defaultName == null)
            name = "data";
        if (name.indexOf(".") == -1)
            name += "." + defaultExtension;
        if (currentDirectory == null)
            currentDirectory = System.getProperty("user.dir");
        return getSaveAsFile(name, currentDirectory);
    }// w  w w  . j  av a 2s . com

    public static File getSaveAsFile(String name, String currentDirectory) {
        if (currentDirectory == null)
            currentDirectory = gLastDirectory;
        JFileChooser fc = new JFileChooser(currentDirectory);
        File directory = new File(currentDirectory);
        File theFile = new File(directory, name);
        fc.setCurrentDirectory(directory);
        fc.setSelectedFile(theFile);
        fc.setDialogType(JFileChooser.SAVE_DIALOG);
        int ret = fc.showSaveDialog(null);
        if (ret == JFileChooser.APPROVE_OPTION) {
            File selectedFile = fc.getSelectedFile();
            gLastDirectory = selectedFile.getParent();
            return selectedFile;
        } else {
            return null;
        }
    }
}

Related

  1. getNewFileChooser()
  2. getOpenFile(String defaultPath)
  3. getOpenFile(String name, String currentDirectory)
  4. getOpenFile(String title)
  5. getPropertiesFile(boolean saving, String startName, String extension, String description)
  6. getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
  7. getSelectedFiles(final JFileChooser chooser)
  8. getSelectedFiles(JFileChooser chooser)
  9. getSelectedFileWithExtension(JFileChooser c)