简体   繁体   English

允许JFileChooser仅选择特定的文件名格式

[英]Allow JFileChooser to select only specific filename format

Is it possible in JFileChooser to have the FileFilter with wildcards ? JFileChooser中是否可以使用带通配符的FileFilter?

When File browser opens I want the user to select a specific file like *_SomeFixedFormat.def file among all *.def files. 当文件浏览器打开时,我希望用户在所有* .def文件中选择一个特定文件,例如* _SomeFixedFormat.def文件。

With FileNameExtensionFilter I'm able to do it for .def files but not for this specific file. 使用FileNameExtensionFilter,我可以对.def文件执行此操作,但不能对此特定文件执行此操作。

FileNameExtensionFilter fileFilter=new FileNameExtensionFilter(".def", "def");
fileChooser.setFileFilter(fileFilter);

Create your own FileFilter 创建自己的FileFilter

JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileFilter(){
    @Override
    public boolean accept(File file){
        // always accept directorys
        if(file.isDirectory())
            return true;
        // but only files with specific name _SomeFixedFormat.def
        return file.getName().equals("_SomeFixedFormat.def");
    }
    @Override
    public String getDescription() {
        return ".def";
    }
});

Change the FileNameExtensionFilter(".def", def); 更改FileNameExtensionFilter(".def", def); to FileNameExtensionFilter(".def", "_yourFixedFormat.def"); FileNameExtensionFilter(".def", "_yourFixedFormat.def"); . I am not sure if this works. 我不确定这是否有效。 If not, restrict it only to .def, and when you choose a file, check if name of the file equals to your format, if not, open JFileChooser again. 如果不是,则仅将其限制为.def,然后在选择文件时,检查文件名是否等于您的格式,否则,请再次打开JFileChooser。

Usage 用法

DefFileFilter fileFilter=new DefFileFilter (new String[] {"DEfFile1"});
fileChooser.setFileFilter(fileFilter);

Filter 过滤

package ui.filechooser.filter;

import java.io.File;
import javax.swing.filechooser.FileFilter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.io.FilenameUtils;

/**
 *
 * @author Igor
 */
public class DefFileFilter extends FileFilter {

    public final static String DEF_EXT = "def";
    //def file name  example : "DEfFile1", "DefFile2" ....
    private String[] allowedNames;

    public DefFileFilter() {
        this(null);
    }

    public DefFileFilter(String names[]) {
        this.allowedNames = name;
    }

    public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }

        String extension = getExtension(f);
        if (extension != null) {
            if (extension.equals(DEF_EXT)) {
                if(allowedNames != null && !StringUtils.indexOfAny(getBaseName(f.getName), allowedNames)) {
                    return false;
                } else {
                 return true;
                }

            } else {
                return false;
            }
        }

        return false;
    }

    public static String getBaseName(String fileName) {
        int index = fileName.lastIndexOf('.');
        if (index == -1) {
            return fileName;
        } else {
            return fileName.substring(0, index);
        }
    }

     public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');

        if (i > 0 && i < s.length() - 1) {
            ext = s.substring(i + 1).toLowerCase();
        }
        return ext;
    }

    public String getDescription() {
        return "Excel file";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM