I wrote a small program that needs to save and load files, and I wrote a GUI to make it more pleasing to possible users. I am having two distinct problems with the java.awt.FileDialog class. The first also concerns the java.io.FilenameFilter interface.
FileDialog fd = new FileDialog(this, "Open File", FileDialog.LOAD);
fd.setFilenameFilter(new FilenameFilter (){
public boolean accept(File dir, String name) {
return name.endsWith(".rd");
} // accept(File, String)
});
This seems like it should work, but does absolutely nothing. All files show up in the FileDialog, regardless of endings. I added a System.out.println() statement, and indeed it wasn't even being called. This, however, does work:
fd.setFile("*.rd");
No, I don't have both pieces of code active at once, although when I do, it works the same as when I just have the second working. Granted, it's nice this works, but it doesn't seem like it should. And the user can't override the .rd ending requirement, so if they save a file without the .rd ending, they have to change it manually to get it to work.
The second is the FileDialog always opens to the Desktop. If I manually hardcode a file, like say using
FileOutputStream fos = new FileOutputStream(ident + ".rd") ;
it gets saved to the same folder where the code is, which would be my preference. I know that I can use the setDirectory() method, but how do I get the name of the directory? (Obviously I could hardcode a different directory, but that wouldn't be system or installation independent.) So how do I get Java to tell me the name of the folder that the code is in?
Any replies would be appreciated.
Paul Scheiblich
pscheiblich@sc.rr.com