Java JFileChooser getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)

Here you can find the source of getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)

Description

get Save File

License

LGPL

Declaration

public static File getSaveFile(String message, File defaultFileOrDir, String description,
            final String... extensions) 

Method Source Code


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

import javax.swing.*;

import javax.swing.filechooser.FileNameExtensionFilter;

import java.io.File;
import java.io.FilenameFilter;

public class Main {
    public static File getSaveFile(String message) {
        return getSaveFile(message, null, null, (String[]) null);
    }/*from   w  ww  .  j a v  a  2 s.  com*/

    public static File getSaveFile(String message, File defaultFileOrDir, String description,
            final String... extensions) {
        File[] files = getFile(message, false, defaultFileOrDir, false, description, extensions);
        if (files == null) {
            return null;
        } else {
            return files[0];
        }
    }

    public static File[] getFile(String message, boolean bLoadNotSave, File defaultFileOrDir,
            boolean bAllowMultipleSelection, String description, final String... extensions) {
        if (isMac()) {
            java.awt.Frame frame = new java.awt.Frame();
            java.awt.FileDialog chooser = new java.awt.FileDialog(frame, message,
                    (bLoadNotSave ? java.awt.FileDialog.LOAD : java.awt.FileDialog.SAVE));
            if (defaultFileOrDir != null) {
                if (defaultFileOrDir.isDirectory()) {
                    chooser.setDirectory(defaultFileOrDir.getAbsolutePath());
                } else {
                    chooser.setFile(defaultFileOrDir.getAbsolutePath());
                }
            }
            if (description != null) {
                FilenameFilter filter = new FilenameFilter() {
                    @Override
                    public boolean accept(File dir, String name) {
                        for (int i = 0; i < extensions.length; i++) {
                            if (name.toLowerCase().endsWith(extensions[i].toLowerCase())) {
                                return true;
                            }
                        }
                        return false;
                    }
                };
                chooser.setFilenameFilter(filter);
            }

            //        chooser.show();
            chooser.setVisible(true);
            if (chooser.getFile() == null)
                return null;
            java.io.File file = new java.io.File(chooser.getDirectory(), chooser.getFile());
            chooser.dispose();
            frame.dispose();
            return new File[] { file };
        } else {
            // No file name in the arguments so throw up a dialog box...
            java.awt.Frame frame = new java.awt.Frame();
            frame.setTitle(message);
            final JFileChooser chooser = new JFileChooser(defaultFileOrDir);
            chooser.setMultiSelectionEnabled(bAllowMultipleSelection);
            //chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

            if (description != null) {
                FileNameExtensionFilter filter = new FileNameExtensionFilter(description, extensions);
                chooser.setFileFilter(filter);
            }

            if (bLoadNotSave) {
                if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                    frame.dispose();
                    if (bAllowMultipleSelection) {
                        return chooser.getSelectedFiles();
                    } else {
                        if (chooser.getSelectedFile() == null) {
                            return null;
                        }
                        return new File[] { chooser.getSelectedFile() };
                    }
                }
            } else {
                if (chooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION) {
                    frame.dispose();
                    if (bAllowMultipleSelection) {
                        return chooser.getSelectedFiles();
                    } else {
                        if (chooser.getSelectedFile() == null) {
                            return null;
                        }
                        return new File[] { chooser.getSelectedFile() };
                    }
                }
            }
        }
        return null;
    }

    public static boolean isMac() {
        return System.getProperty("os.name").toLowerCase().startsWith("mac");
    }
}

Related

  1. getOpenFile(String defaultPath)
  2. getOpenFile(String name, String currentDirectory)
  3. getOpenFile(String title)
  4. getPropertiesFile(boolean saving, String startName, String extension, String description)
  5. getSaveAsFile(String defaultName, String currentDirectory, String defaultExtension)
  6. getSelectedFiles(final JFileChooser chooser)
  7. getSelectedFiles(JFileChooser chooser)
  8. getSelectedFileWithExtension(JFileChooser c)
  9. getSelectedFileWithExtension(JFileChooser c)