轉:ExampleFileFilter.java 類檔案

來源:互聯網
上載者:User

今天在寫開啟檔案對話方塊的代碼時,想對檔案類型進行限制,在JDK協助文檔中,提供了如下方法:

    JFileChooser chooser = new JFileChooser();
// Note: source for ExampleFileFilter can be found in FileChooserDemo,
    // under the demo/jfc directory in the JDK.
    ExampleFileFilter filter = new ExampleFileFilter();
    filter.addExtension("jpg");
    filter.addExtension("gif");
    filter.setDescription("JPG & GIF Images");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(parent);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
       System.out.println("You chose to open this file: " +
            chooser.getSelectedFile().getName());
    }

在其中用到ExampleFileFilter這個類,繼承了FileFilter這個介面,它放在j2sdk1.4.2_16\demo\jfc\FileChooserDemo\src 中,我用的是jdk1.6版本,偏偏沒有這個檔案,鬱悶了半天,無奈裝了遍j1.4,現在問題解決了,將代碼提取出來如下:

import java.io.File;
import java.util.Hashtable;
import java.util.Enumeration;
import javax.swing.*;
import javax.swing.filechooser.*;
public class ExampleFileFilter extends FileFilter {

    private static String TYPE_UNKNOWN = "Type Unknown";
    private static String HIDDEN_FILE = "Hidden File";

    private Hashtable filters = null;
    private String description = null;
    private String fullDescription = null;
    private boolean useExtensionsInDescription = true;
    public ExampleFileFilter() {
 this.filters = new Hashtable();
    }
    public ExampleFileFilter(String extension) {
 this(extension,null);
    }

    public ExampleFileFilter(String extension, String description) {
 this();
 if(extension!=null) addExtension(extension);
  if(description!=null) setDescription(description);
    } 
    public ExampleFileFilter(String[] filters) {
 this(filters, null);
    }
    public ExampleFileFilter(String[] filters, String description) {
 this();
 for (int i = 0; i < filters.length; i++) {
     // add filters one by one
     addExtension(filters[i]);
 }
  if(description!=null) setDescription(description);
    }
    public boolean accept(File f) {
 if(f != null) {
     if(f.isDirectory()) {
  return true;
     }
     String extension = getExtension(f);
     if(extension != null && filters.get(getExtension(f)) != null) {
  return true;
     };
 }
 return false;
    }
     public String getExtension(File f) {
 if(f != null) {
     String filename = f.getName();
     int i = filename.lastIndexOf('.');
     if(i>0 && i<filename.length()-1) {
  return filename.substring(i+1).toLowerCase();
     };
 }
 return null;
    }
    public void addExtension(String extension) {
 if(filters == null) {
     filters = new Hashtable(5);
 }
 filters.put(extension.toLowerCase(), this);
 fullDescription = null;
    }
    public String getDescription() {
 if(fullDescription == null) {
     if(description == null || isExtensionListInDescription()) {
   fullDescription = description==null ? "(" : description + " (";
  // build the description from the extension list
  Enumeration extensions = filters.keys();
  if(extensions != null) {
      fullDescription += "." + (String) extensions.nextElement();
      while (extensions.hasMoreElements()) {
   fullDescription += ", ." + (String) extensions.nextElement();
      }
  }
  fullDescription += ")";
     } else {
  fullDescription = description;
     }
 }
 return fullDescription;
    }
    public void setDescription(String description) {
 this.description = description;
 fullDescription = null;
    }
    public void setExtensionListInDescription(boolean b) {
 useExtensionsInDescription = b;
 fullDescription = null;
    }
    public boolean isExtensionListInDescription() {
 return useExtensionsInDescription;
    }
}


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.