Its not much, but i found it a bit strange that swing didnt already do this so i made a subclass of jfilechooser that asks the user if they want to replace the file when selecting to save an existing file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| import java.awt.*; import javax.swing.*; import java.io.*;
public class CheckFileChooser extends JFileChooser{ public void approveSelection(){ if(getDialogType() == SAVE_DIALOG){ File sel = getSelectedFile(); if(sel.exists()){ int ow = JOptionPane.showConfirmDialog(this, "The file "+sel.getName()+" already exists, do you want to replace it?", "Replace File?", JOptionPane.WARNING_MESSAGE); if(ow != 0) return; } } super.approveSelection(); } } |