Java JFileChooser promptForFilename(String title)

Here you can find the source of promptForFilename(String title)

Description

Display the showSaveDialog which allows the user to specify file and directory

License

Open Source License

Return

file to be saved

Declaration

public static File promptForFilename(String title) 

Method Source Code


//package com.java2s;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class Main {
    /**// w w w  . j  a v a2s .c o  m
     * Display the showSaveDialog which allows the user to specify file and
     * directory
     * 
     * @return file to be saved
     */
    public static File promptForFilename(String title) {
        File file = null;
        String _title = "Arrah Technology Save File";
        if (title != null || "".equals(title) == false)
            _title = title;

        JFileChooser chooser = new JFileChooser();
        chooser.setDialogTitle(_title);
        chooser.setCurrentDirectory(new File("."));

        int returnVal = chooser.showSaveDialog(null);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
        } else {
            return null;
        }
        if (file.exists()) {
            int response = JOptionPane.showConfirmDialog(null, "Overwrite existing file?", "Confirm Overwrite",
                    JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (response == JOptionPane.CANCEL_OPTION) {
                return null;
            }
        }
        return file;
    }
}

Related

  1. makeFileChooser()
  2. openDataFileChooser(Component com)
  3. openFile()
  4. openFile(String fileExtension)
  5. promptFile(File file, FileFilter filter, String title)
  6. PromptForFileOpen(Component c)
  7. promptSaveJFileChooser(String suggestedFileName, FileNameExtensionFilter... fileNameExtensionFilters)
  8. readAsString(File file)
  9. readAsString(File file)