FileNameExtensionFilter多参数构造函数Lint报错未定义,如何修复?
Let's break down what's going wrong here and how to fix it quickly.
First, let's look at your problematic code:
FileNameExtensionFilter filter = new FileNameExtensionFilter("*.Images", "jpg", "png", "gif", "jpeg");
The lint error The constructor FileNameExtensionFilter(String, String, String, String, String) is undefined pops up because you're misusing the constructor of javax.swing.filechooser.FileNameExtensionFilter. Here's what you need to adjust:
- Fix the first parameter: The first argument isn't a wildcard filter string (like
*.Images), it's a human-readable description of the file type. For example, use"Image Files"instead of"*.Images". - Confirm the constructor logic: The class uses a varargs parameter for extensions, so passing multiple extension strings is totally allowed—your issue isn't the number of extensions, it's the invalid first parameter.
Here's the corrected code:
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "gif", "jpeg");
Why does this work?
The official constructor signature for FileNameExtensionFilter is:
public FileNameExtensionFilter(String description, String... extensions)
- The
descriptionis what users see in the file chooser dropdown (e.g., "Image Files" is much clearer than a cryptic wildcard string). - The
extensionsare the file suffixes to filter, without the leading dot—the class handles adding the dot internally when applying the filter.
If you still see the error after this fix, double-check that you've imported the correct class:
import javax.swing.filechooser.FileNameExtensionFilter;
Avoid importing any other custom or third-party FileNameExtensionFilter classes that might have different constructor signatures.
内容的提问来源于stack exchange,提问作者Amant Gupta

