Google Tag Manager

2012/08/17

JFileChooser with file already exists Dialog

Code

JFileChooser fileChooser = new JFileChooser() {
  @Override public void approveSelection() {
    File f = getSelectedFile();
    if(f.exists() && getDialogType() == SAVE_DIALOG) {
      String m = String.format(
          "<html>%s already exists.<br>Do you want to replace it?",
          f.getAbsolutePath());
      int rv = JOptionPane.showConfirmDialog(
          this, m, "Save As", JOptionPane.YES_NO_OPTION);
      if(rv!=JOptionPane.YES_OPTION) {
        return;
      }
    }
    super.approveSelection();
  }
};

References