Java JFileChooser showFileChooserLoop(Component parentComponent, JFileChooser chooser)

Here you can find the source of showFileChooserLoop(Component parentComponent, JFileChooser chooser)

Description

show File Chooser Loop

License

Open Source License

Declaration

public static int showFileChooserLoop(Component parentComponent, JFileChooser chooser) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;
import java.io.File;

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

public class Main {
    public static int showFileChooserLoop(Component parentComponent, JFileChooser chooser) {
        for (;;) {
            int option = chooser.showOpenDialog(parentComponent);
            if (option != JFileChooser.APPROVE_OPTION) {
                return JOptionPane.CANCEL_OPTION;
            }/*from www .ja v  a  2s.  co  m*/

            File file = chooser.getSelectedFile();
            if (file.exists()) {
                int op2 = showReplaceExistingFileConfirmDialog(parentComponent, file);
                if (op2 == JOptionPane.YES_OPTION) {
                    return JOptionPane.YES_OPTION;
                } else if (op2 == JOptionPane.CANCEL_OPTION) {
                    return JOptionPane.CANCEL_OPTION;
                }
            } else {
                return JOptionPane.YES_OPTION;
            }
        }
    }

    public static int showReplaceExistingFileConfirmDialog(Component parentComponent, File file) {
        return JOptionPane.showConfirmDialog(parentComponent,
                "Do you want to replace the existing file " + file + " ?", "Warning",
                JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    }
}

Related

  1. setCurrentDirectory(JFileChooser fileChooser, String location)
  2. setFile(File file, JFileChooser chooser)
  3. setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)
  4. setSelectedFile(JFileChooser chooser, File file)
  5. SHOW_JFILECHOOSER(String title, String approvetext, int fileselectionmode, boolean multiselection)
  6. write(File file, String text)
  7. writeOnFile(String row)
  8. writeStringToFile(String output)