Java JFileChooser selectNonExistingFile(Component parent, String extensionWanted)

Here you can find the source of selectNonExistingFile(Component parent, String extensionWanted)

Description

select Non Existing File

License

Open Source License

Declaration

public static String selectNonExistingFile(Component parent, String extensionWanted) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Component;

import java.io.*;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

public class Main {
    private static File lastChooserPath;

    public static String selectNonExistingFile(Component parent, String extensionWanted) {
        String forReturn = null;/*from ww  w. jav a 2s.co  m*/
        final String endWith = extensionWanted;

        JFileChooser chooser = new JFileChooser(lastChooserPath);
        chooser.setFileFilter(new FileFilter() {
            @Override
            public boolean accept(File file) {
                String filename = file.getName();
                return (filename.endsWith(endWith) || file.isDirectory());
            }

            @Override
            public String getDescription() {
                return endWith;
            }
        });
        int result = chooser.showSaveDialog(parent);
        if (result == JFileChooser.APPROVE_OPTION) {
            try {
                lastChooserPath = chooser.getCurrentDirectory();
                forReturn = chooser.getSelectedFile().getCanonicalPath();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (forReturn != null) {
            if (!forReturn.endsWith(extensionWanted)) {
                forReturn += extensionWanted;
            }
        }
        return forReturn;
    }
}

Related

  1. selectedFiles(JFileChooser chooser)
  2. selectFile(boolean exitOnCancel)
  3. selectFile(Component parent, boolean isOpen)
  4. selectFile(String msg, File dir, FileFilter filterOrNull)
  5. selectFileForOpen(final Component parent, final FileFilter[] fileFilters, final String title, final FileFilter[] selectedFilters, final File initFile)
  6. setCurrentDirectory(JFileChooser fileChooser, String location)
  7. setFile(File file, JFileChooser chooser)
  8. setFileChooserFilters(JFileChooser fileDlg, String filters, int selectIndex)
  9. setSelectedFile(JFileChooser chooser, File file)