声明的扩展文件未显示在JFileChooser窗口中。这是我的过滤器类:
import java.io.File;
public class AudioFilter extends javax.swing.filechooser.FileFilter{
public boolean accept(File f){
if (f.isDirectory()){
return true;
}
String extension = Utils.getExtension(f);
if (extension != null){
if (extension.equals(Utils.wav)
|| extension.equals(Utils.aif)
|| extension.equals(Utils.rmf)
|| extension.equals(Utils.au)
|| extension.equals(Utils.mid)){
return true;
}else{
return false;
}
}
return false;
}
public String getDescription(){
return "wav, aif, rmf, au, mid";
}
}
class Utils{
public final static String wav = "wav";
public final static String aif = "aif";
public final static String rmf = "rmf";
public final static String au = "au";
public final static String mid = "mid";
/*
* 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;
}
}
这是我的代码来调用过滤过程:
AudioFilter audiofiler = new AudioFilter();
boolean openFile() throws FileNotFoundException{
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("Open File");
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setCurrentDirectory(new File ("."));
jfc.setFileFilter(audiofilter);
int result = jfc.showOpenDialog(this);
if(result == JFileChooser.CANCEL_OPTION){
return true;
}else if(result == JFileChooser.APPROVE_OPTION){
fFile = jfc.getSelectedFile();
String file_string = readFile (fFile);
if(fFile.isDirectory()){
String[] filesInDirectory = fFile.list();
for(int i=0;i<filesInDirectory.length;i++){
jList1.setModel(list);
list.addElement(filesInDirectory[i]);
}
}
if(file_string != null){
fTextArea.setText(file_string);
}else{
return false;
}
}
return true;
}
最佳答案
我也测试过,它工作正常。如果仍然有问题,请发布完整的代码。