I would like to show with JFileChooser a list of all the files in a given folder, but ONLY those which start with a certain string, independently of they ending extension. This is because I have a folder with files which have the same name for 3-4 different extension, let's say i.e.: <<"customers.xls, customers.doc, customers.pdf, sales.xls, sales.txt, sales.doc...>> The program will get a certain file name from an input/user, and I will like to get shown all the files which matched the given name. As far as I managed untill now, is to find how to filter only on extensions, which does not manage what I need... Any hint on this? Thanks!
Asked
Active
Viewed 215 times
0
-
Take a look [here](https://stackoverflow.com/questions/15330285/how-to-make-jfilechooser-display-only-a-folder-that-has-some-specific-name-java?rq=1). This is a solution which works with directories, but you can adapt the used File Filter. – Maia Jan 19 '18 at 11:21
2 Answers
0
Create a custom FileFilter for it:
JFileChooser jfc = ...;
String filePrefix = "customers";
FileFilter ff = new FileFilter() {
public boolean accept(File f) {
return f.getName().startsWith(filePrefix);
}
public String getDescription(){
return "Custom Files";
}
}
jfc.setFileFilter(ff);

Georg Leber
- 3,470
- 5
- 40
- 63
-
-
@PaulEfford It would be nice, when you accept an answer that works for you. – Georg Leber Jan 19 '18 at 15:11
0
Use FileFilter :
Definition:
class MyFilter implements FileFilter {
String prefix;
void setPrefix(String prefix) {
this.prefix = prefix
}
public String getDescription() {
return "MyFiles";
}
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
} else {
return f.getName().startsWith(prefix);
}
}
}
Usage:
MyFilter myFilter = new MyFilter();
myFilter.setPrefix("customer");
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(myFilter);