扩展文件不显示在 JFileChooser 上
声明的扩展文件不会显示在 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;
}
Declared extension files doesn't show on JFileChooser window. Here is my filter class:
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;
}
}
Here is my code to call the filter process:
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我也测试过,效果很好。如果您仍然遇到问题,请发布您的完整代码。
I tested it too, it works fine. Post your full code, if you are still having problem.
我测试了您的代码并修改了一些代码以进行文件过滤的简单测试(将选定的文件打印到标准输出),如下所示,它对于
AudioFilter
对象工作得很好...AudioFilter.java:
AudioFilterTest.java:
我想您需要检查您的
openFile()
方法的版本......特别是。你的 readFile(File) 方法I tested your codes and modified a few for a simple test of file filtering (print out the selected file to std out) as follows and it works just fine for the
AudioFilter
object...AudioFilter.java:
AudioFilterTest.java:
I guess you need to check your version of
openFile()
method...esp. yourreadFile(File)
method