JFileChooser 帮助
我正在尝试为 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
我该如何解决这个问题?
I am trying to set the file filter for my JFileChooser. This is my code:
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);
}
When I run my program, the file chooser comes up, but it won't let me pick any .txt files. Instead, it says this in the console:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Extensions must be non-null and not empty
How do i fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要添加至少一个扩展作为第二个参数。从API:
You need to add at least one extension as a second paramter. From the API:
另外,如果您想要特定的文件扩展名并浏览文件夹,您可以尝试以下操作:
Also if you want an specific files extensions and navigate thru folders you can try this: