We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi members,
I am using selectFolder() for the folder path and FilenameFilter class to filter some file extensions. The filter is working fine and i can get the filtered file names with println() from the selected folder. But request an image with that name inside the folder simply won't work. Every time i do this i get NullPointerException. I also use the controlP5 library for a textfield and a button.
import controlP5.*;
import java.io.File;
import java.io.FilenameFilter;
ControlP5 dirPath, dirButton;
PImage[] defImage;
String filter;
File[] listFiles, listFolders;
String selectedAbsolutePath, selectedAbsolutePathUnix, selectedFolderName;
String[] dirSubfolders, dirFiles;
void setup() {
size(600, 600, P3D);
background(255);
fill(255);
PFont font = createFont("Calibri-48.vwl", 12);
dirPath = new ControlP5(this);
dirButton = new ControlP5(this);
dirPath.addTextfield("input")
.setPosition(20, 20)
.setSize(200, 30)
.setLabel("")
.setAutoClear(false)
.setFont(font)
.setFocus(true)
.setColor(color(255, 255, 255));
//OpenDirectory is the button ID. setLabel gives the name to be viewed
dirButton.addButton("OpenDirectory")
.setLabel("Open Directory")
.setPosition(250, 25)
.updateSize();
}
void draw() {
background(0);
//text(String.valueOf(selectedFolderName), 100, 400);
}
void checkFilter() {
Filter filter = new Filter(".jpg");
File parentDir = new File(selectedAbsolutePath);
String[] flist = parentDir.list(filter);
for (int i=0; i < flist.length; i++) {
println(selectedAbsolutePathUnix + "/" + flist[i]);
try {
defImage[i] = requestImage(selectedAbsolutePathUnix + "/" + flist[i]);
image(defImage[i], 200, 200);
}
catch(Exception e) {
e.printStackTrace();
}
println(flist[i]);
}
}
class Filter implements FilenameFilter {
String extension;
Filter(String extns) {
extension = extns;
}
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(this.extension);
}
}
// Methods is connected with selectFolder()
void setDirectoryPath(File selection) {
if (selection != null) {
selectedAbsolutePath = selection.getAbsolutePath();
selectedAbsolutePathUnix = selectedAbsolutePath.replace("\\", "/");
selectedFolderName = selection.getName();
// send the absolute path to the textfield input
dirPath.get(Textfield.class, "input").setText(selectedAbsolutePath);
checkFilter();
}
else {
println("Windows was or user hit cancel.");
}
}
// Button event directory selection
void OpenDirectory() {
//println("Directory selection is pressed");
selectFolder("Select a folder to process:", "setDirectoryPath");
}
Answers
Funny in my example below it worked alright.:
http://forum.processing.org/two/discussion/2578/how-to-filter-and-order-a-set-of-files
I've used an anonymous instantiation rather than make a custom class outta FilenameFilter though. 8-X
Thanks GoToLoop :).