Need FileDialog with file type filter in Java

I have a JDialog with a button / text box so the user can select a file. Here is 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 looking at the "All Files" filter, I want to provide a custom filter, for example. for Word documents or something else. I am setting up the FilenameFilter file using setFilenameFilter (), but it does not seem to work. I noticed that the docs say that the user filter does not work on Windows (this runs on Windows XP / Vista / 7). Here is my filter implementation:

 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? In addition, I want the description to appear in a field, such as "Microsoft Word (* .doc * .docx)," but I'm not sure how to do this.

Any help is appreciated.

+4
source share
6 answers

AWT is actually not the preferred way to write Java GUI applications these days. The sun seems to have basically abandoned it. The two most popular options are Swing and SWT . Therefore, I think that they did not develop the API very widely to add modern features. (err to answer your question: No, you don't seem to be able to do this with AWT)

The advantage of Swing is that it really works with recording once, and everywhere, it can look the same. There are Look and Feels who are trying to make Swing look native, some are better than others (Mac is not scary, Windows is fine, GTK is not). However, if you want the app to really look and act EXACTLY the same everywhere, Swing lets you do it. In addition, it comes without a box without any additional libraries. Performance is low.

Swing JFileChooser lets you do what you want. Subclass FileFilter and call setFileFilter on JFileChooser .

SWT takes a write once in the opposite direction in the opposite direction. You still have one code base with which you write, but in fact it uses its own widgets on each platform, so it usually looks like a native application (not perfect everywhere, but still impressive). It is fast and fairly reliable in my experience. Eclipse (and other high-profile software) uses SWT, so it is quite actively used. But this requires platform-specific JARs and DLLs.

+5
source

since you are using JDialog, it is a swing class, why not use 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.

+5
source

I am also trying to do this. 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 does not work. We must use the setFile method to set the filter."

There is source code at the specified link.

I tested and works:

 FileDialog fd = new FileDialog((Frame) null, "Save File", FileDialog.SAVE); fd.setFile("*.txt"); fd.setVisible(true); String file = fd.getFile(); System.out.println(file); System.exit(0); 
+3
source

You can invoke your own Windows Filedialog (CFileDialog) with JNI. Filters can be easily installed for CFileDialog.

I wrote a simple wrapper class for CFileDialog a few months ago. If you're interested, you can get the source and binary from

Xfiledialog project in google code

+2
source

If you've ever used JavaFX 2 , FileChooser will do what you need without any JFileChooser / FileDialog problems. You can also embed JavaFX 2 components in Swing applications, but you need JavaFX runtime .

Example:

  FileChooser fc = new FileChooser(); FileChooser.ExtensionFilter filter; filter = new FileChooser.ExtensionFilter("Text files (*.txt)", "*.txt"); fc.getExtensionFilters().add(filter); File f = fc.showOpenDialog(primaryStage); System.out.println(f); 
+1
source

Just use the setFilenameFilter method of the FileDialog fd instance:

  fd.setFilenameFilter(new FilenameFilter() { @Override public boolean accept(File file, String s) { // enter code to return TRUE or FALSE here return s.contains(".txt"); } }); 
0
source

All Articles