4

I may be missing something obvious in the JFileChooser API, but when I try and use a JFileChooser to save a file, I can only select pre-existing files to save to, not enter a new name and save to that. Is that even possible with a JFileChooser or should I be using a different API?

I have this code to try and do what I'm attempting:

public static File getUserFile() {      
    final SaveFileChooser fc = new SaveFileChooser();

    fc.setAcceptAllFileFilterUsed(false);

    for(FileFilter ch : FileFilterUtils.getAllFilters()) {
        fc.addChoosableFileFilter(ch);
    }       

    int option = fc.showSaveDialog(JPad.getFrame());

    if (option == JFileChooser.APPROVE_OPTION) {
        return fc.getSelectedFile();
    } 
    return null;
}

public static class SaveFileChooser extends JFileChooser {
    private static final long serialVersionUID = -8175471295012368922L;

    @Override
    public void approveSelection() {
        File f = getSelectedFile();
        if(f.exists() && getDialogType() == SAVE_DIALOG){
            int result = JOptionPane.showConfirmDialog(JPad.getFrame(), "The file exists, overwrite?", "Existing file", JOptionPane.YES_NO_CANCEL_OPTION);

            switch(result){
            case JOptionPane.YES_OPTION:
                super.approveSelection();
                return;
            case JOptionPane.NO_OPTION:
                return;
            case JOptionPane.CLOSED_OPTION:
                return;
            case JOptionPane.CANCEL_OPTION:
                cancelSelection();
                return;
            }
        }
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Timr
  • 1,012
  • 1
  • 14
  • 29
  • Check [this](http://stackoverflow.com/questions/2377703/save-file-with-jfilechooser-save-dialog) post and [this](http://stackoverflow.com/questions/1401285/save-using-jfilechooser-with-pre-populated-file-name?rq=1) –  Dec 06 '12 at 07:16
  • There is an example made by oracle: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/uiswing/examples/components/FileChooserDemoProject/src/components/FileChooserDemo.java – Aksel Willgert Dec 06 '12 at 07:34

2 Answers2

4

Check your if condition:

if(f.exists() && getDialogType() == SAVE_DIALOG)

What happens if f doesn't exist (which is what you would like to be possible)?

You could try:

if(getDialogType() == SAVE_DIALOG) {
    if(f.exists()) {
        // your overwrite checking
    } else {
        super.approveSelection();
        return;
    }
}
ncenerar
  • 1,517
  • 12
  • 25
1

try this

    File file = null;
    String path = "";
    JFileChooser chooser = new JFileChooser();
    chooser.addChoosableFileFilter(new ImageFileFilter());
    int returnVal = chooser.showOpenDialog(null);

    if (returnVal == JFileChooser.APPROVE_OPTION) {
        file = chooser.getSelectedFile();
        path = file.getPath();

        repaint();

    }

}                                        

class ImageFileFilter extends FileFilter {

    public boolean accept(File file) {
        if (file.isDirectory()) {
            return false; //or ur code what file u want to return
        }}
Shiv
  • 4,569
  • 4
  • 25
  • 39
  • 2
    Can you explain your solution a little more? It is not a SSCCE and I don't see the relation with the original code... – ncenerar Dec 06 '12 at 09:35
  • 1
    i am new to java i created an application in netbeans to browse files to save and i tried this code which lets me to create a new folder also so i suggested this code :) if it can help if not i am really sorry – Shiv Dec 06 '12 at 09:48
  • this will not work, it should invoke `showSaveDialog` or the user will not have any ability to type in a new file name – Edoardo Jul 23 '18 at 07:38