I have a JDialog with a button/textfield for the user to select a file. Here's the code:
FileDialog chooser = new FileDialog(this, "Save As", FileDialog.SAVE );
String startDir = saveAsField.getText().substring( 0, saveAsField.getText().lastIndexOf('\\') );
chooser.setDirectory(startDir);
chooser.setVisible(true);
String fileName = chooser.getFile();
My problem is that instead of seeing an All Files filter, I want to provide a custom filter, e.g. for Word docs or something. I setup a custom FilenameFilter using setFilenameFilter(), but it didn't seem to work. I did notice that it says in the docs that the custom filter doesn't work in Windows (this runs in Windows XP/Vista/7). Here was my implementation of the filter:
chooser.setFilenameFilter( new geFilter() );
public class geFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return name.endsWith( ".doc" ) || name.endsWith( ".docx" );
}
}
Am I doing something wrong here? Also, I want a description to appear in the box, like "Microsoft Word (*.doc *.docx)" but I'm not sure how to do that.
Any and all help is appreciated.