Here you can find the source of getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
public static File getSaveFile(String message, File defaultFileOrDir, String description, final String... extensions)
//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"); } }