I'm currently trying to add File Filter through the use of arrays (1D for description and 2D for the extension) as follows:
void findFile(){
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Choose file to upload");
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed(true);
ft = new FileType(this.chooser);
... //rest of code
}
FileType.java
package function;
import java.io.FileFilter;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class FileType {
private String[] desc = {
"Word Document (.doc, .docx)", "Excel Document (.xlsx, .xls)"
};
private String[][] ext = {
{"doc", "docx"}, {"xlsx", "xls"}
};
public int counts = desc.length;
FileNameExtensionFilter fe;
JFileChooser session;
public FileType(JFileChooser session){
this.session = session;
generateCode();
}
void generateCode(){
for(int i = 0; i < counts; i++){
for(String pass : ext[i]){
System.out.println(pass);
generateFileType(desc[i], pass);
}
}
}
public void generateFileType(String a, String...b){
for(String x : b){
fe = new FileNameExtensionFilter(a, x);
}
session.addChoosableFileFilter(fe);
}
/*public JFileChooser generateFilter(JFileChooser a){
generateCode();
for(int i = 0; i < counts; i++){
a.addChoosableFileFilter(fe);
}
return a;
}*/
}
Everything (almost) work, but I couldn't figure out how to merge the extensions into one description (i.e. it repeats the description, but with different extension filter). image here.
Did use varargs though, but I can't seem to find a way to pass multiple value at once inside for
loops
Alright, what a stupid of me. This was the answer:
void generateCode(){
for(int i = 0; i < counts; i++){
generateFileType(desc[i], ext[i]);
}
}
public void generateFileType(String a, String...b){
fe = new FileNameExtensionFilter(a, b);
session.addChoosableFileFilter(fe);
}
What I did last time is to iterate over the extension arrays, which made the program passes the value one-by-one.