I am creating a Notepad in Java Netbeans. I have written the code for the File menu (New, Open..., Save and Save As...) and the Edit menu (Copy, Cut and Paste). My problem is that I want that when saving the file if the user chooses the same name as a file that already exists I want him to be asked if he wants to overwrite the existing one or save with another name.
private void abrirVentanaGuardar() {
JFileChooser fc = new JFileChooser();
fc.setMultiSelectionEnabled(false);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
int seleccion = fc.showSaveDialog(this.getContentPane());
// Si hemos pulsado en OK
if (seleccion == JFileChooser.APPROVE_OPTION) {
// Fichero seleccionado
this.ficheroActual = fc.getSelectedFile();
// Se escribe la información en el archivo
this.escribirFichero();
}
}
According to this answer , an elegant way would be to override the JFileChooser method. Something like: