swing 打开文件过滤器


 private File getFile() {
  File txtFile = null;
  JFileChooser fileChooser = new JFileChooser();
  // fileChooser.removeChoosableFileFilter(fileChooser.getFileFilter());
  fileChooser.addChoosableFileFilter(new ExampleFileFilter("txt"));
  fileChooser.addChoosableFileFilter(new ExampleFileFilter("xls"));
  fileChooser.setDialogType(JFileChooser.OPEN_DIALOG);
  int state = fileChooser.showDialog(this, "选择导入文件");
  if (state == JFileChooser.APPROVE_OPTION) {
   txtFile = fileChooser.getSelectedFile();
  }
  return txtFile;
 }
 
 class ExampleFileFilter extends FileFilter {
  String suffix = "";
  ExampleFileFilter(String suffix) {
   this.suffix = suffix;
  }
  public boolean accept(File f) {
   String suffix = getSuffix(f);
   if (f.isDirectory() == true) {
    return true;
   }
   if (suffix != null) {
    if (suffix.toLowerCase().equals(this.suffix)) {
     return true;
    } else {
     return false;
    }
   } else {
    return false;
   }
  }
  public String getDescription() {
   return "*." + this.suffix;
  }
  private String getSuffix(File f) {
   String s = f.getPath(), suffix = null;
   int i = s.lastIndexOf('.');
   if (i > 0 && i < s.length() - 1)
    suffix = s.substring(i + 1).toLowerCase();
   return suffix;
  }
 }

你可能感兴趣的:(swing,F#)