JFileChooser文件过滤

来源:互联网 发布:里海大学 知乎 编辑:程序博客网 时间:2024/05/16 03:43

package main;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2007-12</p>
 * <p>Company: </p>
 * @author samZheng
 * @version 1.0
 */

public class PicFileFilter extends FileFilter {
  public boolean accept(File f) {
    if (f.isDirectory()) {
      return true;
    }

    String jpgName = "jpg";
    String bmpName = "bmp";
    String gifName = "gif";
    String pngName = "png";
    if (getExtension(f).equals(jpgName)||
        getExtension(f).equals(bmpName)||
        getExtension(f).equals(gifName)||
        getExtension(f).equals(pngName))
      return true;
    return false;
  }

  public String getDescription() {
    return "Picture Files";
  }

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

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

原创粉丝点击