Java JFileChooser getFileToSave(String description, String extension, Component component)

Here you can find the source of getFileToSave(String description, String extension, Component component)

Description

get File To Save

License

Open Source License

Declaration

public static File getFileToSave(String description, String extension, Component component) 

Method Source Code


//package com.java2s;
import java.awt.Component;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    public static File getFileToSave(String description, String extension, Component component) {
        JFileChooser fileChooser = new JFileChooser();
        FileFilter filter = new FileNameExtensionFilter(description, new String[] { extension });
        fileChooser.setFileFilter(filter);
        int action = fileChooser.showSaveDialog(component);
        if (action == JFileChooser.APPROVE_OPTION) {
            if (fileChooser.getSelectedFile() != null) {
                File theFile = fileChooser.getSelectedFile();
                if (!theFile.getAbsolutePath().toLowerCase().endsWith("." + extension)) {
                    theFile = new File(theFile.getAbsolutePath() + "." + extension);
                }/*from w w w .j ava  2 s. c  om*/
                return theFile;
            }
        }
        return null;
    }
}

Related

  1. getFileOrDir(String startName, boolean fileSelect)
  2. getFilePath()
  3. getFilePath(Component parent, JFileChooser fileChooser, String title, FileFilter filter)
  4. getFiles(String title, String initialRoot, String initialFile)
  5. getFilesToOpen(File startDirectory, Component parent, String... filters)
  6. getFileWithExtension(JFileChooser c)
  7. getFixedFileChooser(File file)
  8. getIcon(File file)
  9. getJFileChooser(String title, File initialDirectory, File initialFile, final FilenameFilter filter, int directoryMode)