Java tutorial
//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; } 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); } }