我正在尝试为我的 JFileChooser 设置文件过滤器。这是我的代码:
JFileChooser picker= new JFileChooser();
picker.setFileFilter(new FileNameExtensionFilter("txt"));
int pickerResult = picker.showOpenDialog(getParent());
if (pickerResult == JFileChooser.APPROVE_OPTION){
System.out.println("This works!");
}
if (pickerResult == JFileChooser.CANCEL_OPTION){
System.exit(1);
}
当我运行程序时,文件选择器出现,但它不允许我选择任何 .txt 文件。相反,它在控制台中这样说:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Extensions must be non-null and not empty
我该如何解决这个问题?
最佳答案
您需要添加至少一个扩展名作为第二个参数。来自 API:
FileNameExtensionFilter(String description, String... extensions)
Parameters:
description - textual description for the filter, may be null
extensions - the accepted file name extensions
关于java - JFileChooser 帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4881442/