java JFileChooser - 多个文件过滤器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2450223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 21:12:57  来源:igfitidea点击:

JFileChooser - multiple file filters?

javafilesavejfilechooser

提问by bcoughlan

I have a question about the JFileChooser in Swing. I'm trying to get multiple file extensions in the drop-down box, but have no idea how to do it.

我有一个关于 Swing 中的 JFileChooser 的问题。我正在尝试在下拉框中获取多个文件扩展名,但不知道该怎么做。

There is the method

有方法

extFilter = FileNameExtensionFilter(description, extensions);

that I can then use by writing

然后我可以通过写作来使用

fileChooser.setFileFilter(extFilter);

however, as you can see, this only supports one option in the drop-down list. How do I add more?

但是,如您所见,这仅支持下拉列表中的一个选项。我如何添加更多?

回答by Amir Afghani

I think you want the addChoosableFileFiltermethod. Read the tutorial.

我想你想要这个addChoosableFileFilter方法。阅读教程

You can simply create a filter that subclasses FileFilterand call the method I outlined above with that filter as an argument.

您可以简单地创建一个过滤器,它子类化FileFilter并使用该过滤器作为参数调用我上面概述的方法。

回答by clartaq

I'm sure you have already solved this to your satisfaction, but I came across this linkat the same time I came across your question. In particular, look at section 3 for a way to generate multiple filters easily.

我相信你已经解决了这个问题,你满意,但我在遇到你的问题的同时遇到了这个链接。特别是,请查看第 3 节以了解一种轻松生成多个过滤器的方法。

回答by Younes Meridji

use

利用

filter.addChoosableFileFilter(new FileNameExtensionFilter(description, extensions));

as many as the number of your extensions.

与您的扩展程序的数量一样多。