Hello, ok I have a problem with JFileChooser.When I save a file with a name that exists already, It replaces the older file and saves the new one but I want make it show a confirmation message to the user to ask if they want to replace the file or not. Another problem is that I am using both open and save dialog boxes from JFileChooser to open and save ONLY '.xls' i.e excel files. I have created a fileFilter for this purpose which works well with the open dialog box but when saving a file, I want to force the '.xls' extension to be appended with the file name so I can save ONLY '.xls' files. What it does right now is that it saves the file without an extension which I can later on open with Microsoft Excel using 'Open with..' option and it works fine. But I want to make it an excel file at the time it is saved, Please help.
Here is my fileFilter:

javax.swing.filechooser.FileFilter ff=new javax.swing.filechooser.FileFilter() {
             public boolean accept(File f) {
                 return f.getName().toLowerCase().endsWith(".xls")
                         || f.isDirectory();
             }
             public String getDescription() {
                 return "Excel Files";
             }
             
         };

Hey all!
Got both my problems solved using this code.

int actionDialog = UI.fc.showSaveDialog(null);
           if ( actionDialog == JFileChooser.APPROVE_OPTION )
           {
               File fileName = new File( UI.fc.getSelectedFile( ) + ".xls" );
               if(fileName != null)
               {
                   if(fileName.exists())
                   {
                       actionDialog = JOptionPane.showConfirmDialog(null, "Replace existing file?");
                       while (actionDialog == JOptionPane.NO_OPTION)
                       {
                           actionDialog=UI.fc.showSaveDialog(null);
                           if (actionDialog == JFileChooser.APPROVE_OPTION)
                           {
                               fileName = new File( UI.fc.getSelectedFile( ) + ".xls" );
                               if(fileName.exists())
                               {
                                   actionDialog = JOptionPane.showConfirmDialog(null, "Replace existing file?");
                               }
                           }
                       }
                       if(actionDialog == JOptionPane.YES_OPTION)
                       {
                           WorkbookSettings ws = new WorkbookSettings();
                           WritableWorkbook workbook = Workbook.createWorkbook(fileName, ws);
                           WritableSheet s = workbook.createSheet("Result", 0);
                           writeDataSheet(s);
                           workbook.write();
                           workbook.close();
                       }
                       return;
                   }
                   WorkbookSettings ws = new WorkbookSettings();
                   WritableWorkbook workbook = Workbook.createWorkbook(fileName, ws);
                   WritableSheet s = workbook.createSheet("Result", 0);
                   writeDataSheet(s);
                   workbook.write();
                   workbook.close();
               }
           }

Hope it helps anybody else out there :)

Sory forgot to mention 'UI.fc' is my file chooser. UI is the class where i hav created the JFileChooser and fc is its name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.