bjg
asked on
Using java.io.FilenameFilter
I would like to know how to use java.io.FilenameFilter with a FileDialog class. I only want my FileDialog class to accept files of type .exe. How do I do this?
A filename filter looks something like this:
class exeFilt implements FilenameFilter
{ public boolean accept(File dir,String nm)
{ return(nm.endsWith(".exe") );
}
}
You attach it to the FileDialog with the setFilter method:
FileDialog fd=new FileDialog.....
fd.setFilenameFilter(new exeFilt());
class exeFilt implements FilenameFilter
{ public boolean accept(File dir,String nm)
{ return(nm.endsWith(".exe")
}
}
You attach it to the FileDialog with the setFilter method:
FileDialog fd=new FileDialog.....
fd.setFilenameFilter(new exeFilt());
And as to the second question, FileDialog provides:
public void setDirectory(String dir) {
which you would give the relevant argument (e.g. "c:\\windows")
public void setDirectory(String dir) {
which you would give the relevant argument (e.g. "c:\\windows")
ASKER
1) I have tried the exact code you have here for using FilenameFilter and when I run my app I get the following System.out.println: setFilenameFilter not implemented
2) I have tried setDirectory("C:\\Windows" ) and it still defaults to the current directory which I am running the app from.
What is wrong with these two things?
2) I have tried setDirectory("C:\\Windows"
What is wrong with these two things?
ASKER
I got setDirectory to work and I used setFile("*.exe") to only allow me to accept files of type .exe in the FileDialog. I still do not know what is wrong with the FilenameFilter.
Can't imagine either. If you have the source you can see it right in the FileDialog class, so it should not be able to come up with that particular error. It would not imply that there is anything wrong with the filter. "setFilenameFilter not implemented" hints more at something like a misspelling, miscapitalization of the method or the class. Though, obviously, everything looks fine in your comments here.
Here is a codelet that implements FilenameFilter and it works:
public class extFilter implements FilenameFilter
{
private String ext;
private boolean acceptDir = true;
public extFilter(String extension){
this.ext = ext;
}
public extFilter(String ext, boolean acceptDir){
this.ext = ext;
this.acceptDir = acceptDir;
}
public boolean accept(File dir, String name)
{
if (name.endsWith(ext))
return true;
else
return ((acceptDir) && (new File(dir,name)).isDirector y());
}
}
public class extFilter implements FilenameFilter
{
private String ext;
private boolean acceptDir = true;
public extFilter(String extension){
this.ext = ext;
}
public extFilter(String ext, boolean acceptDir){
this.ext = ext;
this.acceptDir = acceptDir;
}
public boolean accept(File dir, String name)
{
if (name.endsWith(ext))
return true;
else
return ((acceptDir) && (new File(dir,name)).isDirector
}
}
ASKER
this is the code that I have and it didn't work:
class MyFilenameFilter implements FilenameFilter {
public boolean accept(File dir,String name) {
return(name.endsWith(".exe "));
}
}
Should this work if I only want the FileDialog to show and allow files of type .exe?
class MyFilenameFilter implements FilenameFilter {
public boolean accept(File dir,String name) {
return(name.endsWith(".exe
}
}
Should this work if I only want the FileDialog to show and allow files of type .exe?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER