Java Utililty Methods JFileChooser

List of utility methods to do JFileChooser

Description

The list of methods to do JFileChooser are organized into topic(s).

Method

ListretrieveFilesFromDir(File basedir, FileFilter filter)
retrieve Files From Dir
return retrieveFilesFromDir(basedir, filter, true, false);
Filerun_file_chooser(String file_desc, String extension)
rufilchooser
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(file_desc, extension);
chooser.setFileFilter(filter);
int ret_val = chooser.showOpenDialog(null);
if (ret_val == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFile();
} else {
    return null;
...
voidsave(JFileChooser fileChooser, Runnable action)
save
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
    action.run();
FilesaveFile(Component parent, File defaultFile)
Open save file dialog and return the selected fie.
JFileChooser chooser = new JFileChooser();
if (defaultFile != null) {
    chooser.setSelectedFile(defaultFile);
if (chooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
    return chooser.getSelectedFile();
return null;
...
FilesaveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter)
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.
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();
...
StringsaveObject(final Object content, final String description, final String extension, final File file)
Saves a file with a file chooser
final JFileChooser fileChooser = new JFileChooser();
final FileNameExtensionFilter langFilter = new FileNameExtensionFilter(description, extension);
fileChooser.setFileFilter(langFilter);
fileChooser.setSelectedFile(file);
String path = "";
try {
    if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
        path = fileChooser.getSelectedFile().getAbsolutePath();
...
File[]saveSystemFiles(Component owner)
Consistent way to chosing multiple files to save with JFileChooser.
JFileChooser jfc = new JFileChooser();
jfc.setFileHidingEnabled(true);
jfc.setMultiSelectionEnabled(true);
int result = jfc.showSaveDialog(owner);
if (result == JFileChooser.APPROVE_OPTION) {
    return jfc.getSelectedFiles();
return new File[0];
...
StringsaveToFile(Object obj)
save To File
return saveToFile(toJson(obj));
Fileselect_file(Component parent, boolean show_save)
selecfile
return select_file(parent, show_save, null);
voidselectAndSaveToFile(String content, Component parent)
Show dialog for selecting a file and write file content to file
@SuppressWarnings("serial")
JFileChooser fileSaveDialog = new JFileChooser() {
    @Override
    public void approveSelection() {
        File f = getSelectedFile();
        if (f.exists() && getDialogType() == SAVE_DIALOG) {
            int result = JOptionPane.showConfirmDialog(this, "File already exists, overwrite?",
                    "Existing file", JOptionPane.YES_NO_OPTION);
...