通过文件名过滤filechooser [英] Filtering filechooser via filename

查看:203
本文介绍了通过文件名过滤filechooser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FileFilter filter = new FileNameExtensionFilter(JPEG file,jpg ,jpeg); 

但是我想用包含某些字符串的文件来过滤我的文件选择器文件名。

只能选择具有这些字符串的文件,并且此过滤器不可编辑。

解决方案

  public class ImageFilter extends FileFilter {

//接受所有目录和所有jpeg,jpg文件,其文件名有损。
public boolean accept(File f){
if(f.isDirectory()){
return true;
}

字符串扩展= Utils.getExtension(f);
String filename = Utils.getName(f); (扩展名=空){
if((extension.equals(Utils.jpeg)|| extension.equals(Utils.jpg))&& filename.contains(lossy) ){
返回true;
} else {
return false;
}
}

return false;

$ b $ //这个过滤器的描述
public String getDescription(){
returnImages(Lossy);




$ b $这是我的utils类$ / $> b
$ b

  public class Utils {
public final static String jpeg =jpeg;
public final static jpg =jpg;

/ *
*获取文件的扩展名。
* /
public static String getExtension(File f){
String ext = null;
String s = f.getName();
int i = s.lastIndexOf('。'); (i> 0& i< s.length() - 1){
ext = s.substring(i + 1).toLowerCase();


}
return ext;

public static String getName(File f){
String fname = null;
String s = f.getName();
int i = s.length() - s.lastIndexOf('。');
fname = s.substring(0,s.length() - i);

返回fname;
}

/ **返回ImageIcon,如果路径无效,则返回null。 * /
protected ImageIcon createImageIcon(String path){
java.net.URL imgURL = Utils.class.getResource(path);
if(imgURL!= null){
return new ImageIcon(imgURL);
} else {
System.err.println(Could not find file:+ path);
返回null;
}
}
}

来应用,

  fc.addChoosableFileFilter(new ImageFilter()); 


I learned how to use filter using filefilter.

FileFilter filter = new FileNameExtensionFilter("JPEG file", "jpg", "jpeg");

But I want to filter my filechooser with files containing certain strings in them like having "sample" in their filenames.

Only files that does have these strings can be chosen and this filter must not be editable. How can I do it?

解决方案

public class ImageFilter extends FileFilter {

    //Accept all directories and all jpeg, jpg files with lossy in its filename.
    public boolean accept(File f) {
        if (f.isDirectory()) {
            return true;
        }

        String extension = Utils.getExtension(f);
        String filename = Utils.getName(f);
        if (extension != null) {
            if ((extension.equals(Utils.jpeg) || extension.equals(Utils.jpg)) && filename.contains("lossy")) {
                    return true;
            } else {
                return false;
            }
        }

        return false;
    }

    //The description of this filter
    public String getDescription() {
        return "Images (Lossy)";
    }
}

and this is my utils class

public class Utils {
    public final static String jpeg = "jpeg";
    public final static String jpg = "jpg";

/*
 * Get the extension of a file.
 */
    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 static String getName(File f) {
        String fname = null;
        String s = f.getName();
        int i = s.length() - s.lastIndexOf('.');
        fname = s.substring(0,s.length()-i);

        return fname;
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Utils.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}

to apply,

fc.addChoosableFileFilter(new ImageFilter());

这篇关于通过文件名过滤filechooser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆