Re: JFileChooser and existing files

From: Allan Bruce (allanmb_at_TAKEAWAYf2s.com)
Date: 01/12/04


Date: Mon, 12 Jan 2004 18:38:17 -0000


"Andrew Thompson" <andrew64@bigNOSPAMpond.com> wrote in message
news:CLAMb.8247$Wa.5455@news-server.bigpond.net.au...
> "Allan Bruce" <allanmb@TAKEAWAYf2s.com> wrote in message
> news:btu1ve$b1r$1@news.freedom2surf.net...
> | I am using a JFileChooser to select a file which I wish to save
> a String to.
> | This works fine however if the file already exists, my program
> just writes
> | over the file without any warning. Is there an option for the
> JFileChooser
> | to ask if the user wishes to replace the existing file, just
> like a Windows
> | FileChooser?
>
> I am pretty sure the Windows file chooser
> does _not_ do any such checking. It is
> really up to the programmer to check..
>
> if file.exists()
> {
> if ( JOptionPane.showConfirmDialog("Are you
> sure?")==JOptionPane.YES_OPTION )
> (..... write the file)
> }
>

I created my own class that extends the JFileChooser and over-rides the
approveSelection(). This makes it feel much more like the Windows
FileChooser, as it provides the dialog within the FileChooser itself. (By
the Windows FileChooser, I mean the Win32 one which I have used extensively
from C/C++).
I am new to Java, if my code below has a problem, could you please let me
know?
Thanks
Allan

private class MyFileChooser extends JFileChooser
    {
        public MyFileChooser()
        {
            super();
        }

        public MyFileChooser(String baseDir)
        {
            super(baseDir);
        }

        public void approveSelection()
        {
            if(!validateFile(this.getSelectedFile()))
            {
                //show some error message
                return;//the FileChooser wont close until and unless u call
super.approveSelection()
            }
            super.approveSelection();
        }

   private boolean validateFile(File file)
        {
            //do your validation here
            if (file.exists())
            {
                if (JOptionPane.showConfirmDialog(null, "File exists,
overwrite?", "Overwrite File?", JOptionPane.YES_NO_OPTION) ==
JOptionPane.YES_OPTION)
                    return true;
                return false;
            }
            else
  return true;
        }
    }



Relevant Pages

  • Konfigurierung von JFileChooser
    ... JFileChooser fileChooser = new JFileChooser; ... Leider befinden wir uns unter Windows und es ist kein echtes ... Cancel-Button wird nur zur Verwirrung betragen, ... Wenn mir Teil 1 gelingt, dann möchte ich von den Buttons im oberen ...
    (de.comp.lang.java)
  • Re: Help! Swing & Threads
    ... I don't see anywhere it is mentioned that the JFileChooser is modal and *supposed* to block events from other windows. ... Modal windows don't block that thread. ... Runable, and have that object invoke the JFileChooser, then call invokeLaterwith object A, is the EDT going to get terribly confused? ...
    (comp.lang.java.help)
  • Re: Strange null-pointer
    ... It would seem your JRE install is broken; the Windows L&F cannot find ... the icon images it needs for JFileChooser. ...
    (comp.lang.java.programmer)
  • Re: JFileChooser leaves grey area after opening file.
    ... contained within another JPanel which makes up a tab from a JTabbed ... JFileChooser was contained in. ... You probably need to call repaintfor the JPanel in which the FileChooser was added but why embed the JFileChooser in a JPanel? ...
    (comp.lang.java.gui)
  • JFileChooser, FileDialog for file saves
    ... I have used both JFileChooser and FileDialog for saving files and neither is ... With FileChooser the dialog is centered ... over the parent however if an existing file is selected a warning message ... I would like to keep the warning with my ...
    (comp.lang.java.gui)