ProfileFileChooser extends JFileChooser { @Override public void approveSelection() { if (getDialogType() == SAVE_DIALOG) { int confirm = JOptionPane.YES_OPTION; File selectedFile = getSelectedFile(); if (selectedFile != null && selectedFile.exists()) { confirm = JOptionPane.showConfirmDialog( this, "The file you have selected already exists. Do you wish to overwrite it?", "File exists warning", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE ); } if (confirm == JOptionPane.YES_OPTION) { super.approveSelection(); } else if (confirm == JOptionPane.CANCEL_OPTION) { super.cancelSelection(); } } else { super.approveSelection(); } }  @Override void approveSelection();  }
@Test @Ignore public void testApproveWhenFileExists() { fc.setDialogType(JFileChooser.SAVE_DIALOG); fc.setSelectedFile(new File("/")); fc.approveSelection(); }