views:

1178

answers:

3

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.

+2  A: 

AWT isn't really the preferred way of writing Java GUI apps these days. Sun seems to have mostly abandoned it. The two most popular options are Swing and SWT. So I think they didn't really develop the APIs very extensively to add modern features. (err, to answer your question: No you don't appear to be able to do that with AWT)

Swing has the advantage that it is truly write-once-run-anywhere and it can look exactly the same everywhere. There are Look & Feels that try to make Swing look native, some are better than others (Mac isn't terrible, Windows is okay, GTK isn't). Still, if you want an app that really looks and acts EXACTLY the same everywhere, Swing will let you do that. Plus it runs out-of-the-box without any extra libraries. Performance isn't great.

Swing's JFileChooser will let you do what you want. Create a subclass of FileFilter and call setFileFilter on the JFileChooser.

SWT takes the write-once-run-anywhere to the opposite extreme. You still have one codebase that you write against, but it actually uses the native widgets on each platform so it generally looks like a native app (not perfect everywhere, but still impressive). It's fast and pretty reliable in my experience. Eclipse (and other high profile software) uses SWT so it's in pretty heavy use. But it does require platform-specific JARs and DLLs.

Adam Batkin
This turned out to be the correct answer for me, and I even eventually figured out how to style the JFileChooser to match the rest of the app. Apparently this just isn't possible with a FileDialog... that still makes me sad.
Morinar
+1  A: 

since you are using JDialog, that is a swing class why not using JFileChooser?

 JFileChooser fc = new JFileChooser("C:\\");
 fc.setFileFilter(new FileNameExtensionFilter("Microsoft Word (*.doc, *.docx)", "doc", "docx"));

FileNameExtensionFilter is a nice Java 6 class that does exactly what you want.

dfa
The reason I'm using FileDialog is that it perfectly matches the Windows look and feel and is also consistent with other dialogs in our application. Can I make a JFileChooser that matches?
Morinar
A: 

You can call the native Windows Filedialog (CFileDialog) with JNI. Filters can be set for CFileDialog easily.

I wrote a simple wrapper class for CFileDialog several months ago, If you are interested, you can get the source and binary from

Xfiledialog project on google code

stevpan