Take the 2-minute tour ×

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('\\') );
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.

share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

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.

share|improve this answer
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 Aug 12 '09 at 15:14
add comment

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.

share|improve this answer
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 Aug 7 '09 at 15:04
add comment

I am also trying to do that. I want to use FileDialog instead of JFileChooser.

I found the answer here: http://www.rgagnon.com/javadetails/java-0247.html

He says that "on the Win platform, the setFilenameFilter method don't work. We must use the setFile method instead to set a filter."

There is source code at the specified link.

I tested and it works:

FileDialog fd = new FileDialog((Frame) null, "Save File", FileDialog.SAVE);

String file = fd.getFile();
share|improve this answer
add comment

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

share|improve this answer
add comment

If you ever use JavaFX 2, the FileChooser class will do exactly what you need without any of JFileChooser/FileDialog problems. You can also embed JavaFX 2 components inside Swing applications, but you need JavaFX runtime.


    FileChooser fc = new FileChooser();
    FileChooser.ExtensionFilter filter;
    filter = new FileChooser.ExtensionFilter("Text files (*.txt)", "*.txt");
    File f = fc.showOpenDialog(primaryStage);
share|improve this answer
add comment

Your Answer


