Java JFileChooser saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter)

Here you can find the source of saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter)

Description

Open a javax.swing.JFileChooser which can be used to save a new file, and return the path to the new file, or null if closed or cancelled.

License

Apache License

Parameter

Parameter Description
title The title of the file-chooser window.
currentDir The root directory. If null, new File(".") will be used.
filter The file filter to use.

Return

The new file, or null if none was chosen.

Declaration

public static File saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter) 

Method Source Code


//package com.java2s;
/*/*from  w  ww.ja  v a  2 s .  com*/
 * Copyright 2014 Aritz Lopez
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

import javax.swing.*;
import java.io.File;

public class Main {
    /**
     * Open a {@link javax.swing.JFileChooser} which can be used to save a new file, and return the path to the new file, or null if closed or cancelled.
     *
     * @param title      The title of the file-chooser window.
     * @param currentDir The root directory. If null, {@code new File(".")} will be used.
     * @return The new file, or {@code null} if none was chosen.
     */
    public static File saveFile(String title, File currentDir) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(currentDir);
        fileChooser.setDialogTitle(title);
        fileChooser.setMultiSelectionEnabled(false);
        int result = fileChooser.showSaveDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFile();
        }
        return null;
    }

    /**
     * Open a {@link javax.swing.JFileChooser} which can be used to save a new file, and return the path to the new file, or null if closed or cancelled.
     *
     * @param title      The title of the file-chooser window.
     * @param currentDir The root directory. If null, {@code new File(".")} will be used.
     * @param filter     The file filter to use.
     * @return The new file, or {@code null} if none was chosen.
     */
    public static File saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter) {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setCurrentDirectory(currentDir);
        fileChooser.setDialogTitle(title);
        fileChooser.setMultiSelectionEnabled(false);
        fileChooser.setFileFilter(filter);
        int result = fileChooser.showSaveDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFile();
        }
        return null;
    }
}

Related

  1. removeChoosableFileFilters(JFileChooser fc)
  2. retrieveFilesFromDir(File basedir, FileFilter filter)
  3. run_file_chooser(String file_desc, String extension)
  4. save(JFileChooser fileChooser, Runnable action)
  5. saveFile(Component parent, File defaultFile)
  6. saveObject(final Object content, final String description, final String extension, final File file)
  7. saveSystemFiles(Component owner)
  8. saveToFile(Object obj)
  9. select_file(Component parent, boolean show_save)