Reputation:
I'm programming a simple sketch in processing (a type of java), and using this bit of code to scan a folder for jpgs. I would like to be able to use it as a function, so I can use it like this:
String[] images;
void setup() {
images = scanForJpgs();
}
String[] scanForJpgs() {
// set target folder
java.io.File folder = new java.io.File(dataPath(""));
// set filter (which returns true if file's extension is .jpg)
java.io.FilenameFilter jpgFilter = new java.io.FilenameFilter() {
boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".jpg");
}
};
// list files in target folder, passing the filter as parameter
String[] filenames = folder.list(jpgFilter);
return filenames;
}
But it throws a "Cannot reduce the visibility of the inherited method from FilenameFilter" error. How do I fix this so I can use it like a function?
Upvotes: 1
Views: 1308
Reputation: 159844
Declare the method public
rather than package-private:
public boolean accept(File dir, String name) {
Read: Controlling Access to Members of a Class
Upvotes: 2