Working with files is a moderately common task in Java. For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.
However, when your application has visual components, it might be a good idea to allow the user to select a file in a way similar to the Internet Browser's file choosers.
In a simple scenario, the usage of javax.swing.JFileChooser is all that is necessary to allow the user to browse the files and folders of the underlying Operating System. But in most projects, the files that should be selected should have some specific properties - its location might be predefined, it might be restricted by size or by name, etc. One option to solve these cases would be to allow the user to select any file or directory and then to check if this file/directory meets the requirements, notifying user for those that do not. But a much better approach would be to show the user only the files that they can select from in the first place.
The following sections will take us through some code showing how the JFileChooser can be used in these more complicated scenarios. Consequently, since every time a JFileChooser is used, a hidden object - a FileFilter - is used also. This FileFilter is responsible for deciding which files are to be shown in the UI. Therefore, this article will show you how to create your own FileFilters.
- Adding a Custom File Filter.
- Custom File Filtering: Using Anonymous FileFilter Implementation.
- Custom File Filtering: Using Named FileFilter Implementation.
- Wrapping Things Up.
PRE-REQUISITES
This article is for beginner Java programmers, so don't worry as the only pre-requisites are the Java Developer's Kit (JDK), version 1.2 or higher, and a development tool of your choice. Although, notepad is good enough, I would recommend an Integrated Development Environment (IDE) such as NetBeans or Eclipse to ease the trivial tasks of compilation and to provide niceties like color-coded syntax. For a balance between color-coding and the purity of using notepad, TextPad or NotePad++ would be a nice alternative on Windows. Downloads for some editors will be listed at the end of the article; however, please feel free to use whichever tools you are comfortable with.
- 1
Adding a Custom File Filter.
To begin our journey, we will examine the basics of properly adding a FileFilter to JFileChooser.
As shown in the code above, the key method is addChoosableFileFilter(jav
Another key to truly having a custom file filter is to turn off the standard "All Files" files of type choice:
- 2
Custom File Filtering: Using Anonymous FileFilter Implementation.
Now let's take a look at a very simplified example using rich text format (RTF) file filter.
We'll start with the getDescription() since it doesn't take much to create:
Tired yet!
That was a lot of code, I know. :)
So considering that should be self explanatory, let's add our accept(File) method:
Voilà !
Not much harder, but a little more to talk about here.
When choosing a file, we want to be able to browse directories in order to traverse them; therefore, we have to ensure that we return true for all folders.
Then for files, we are going to do our filtering based on simple inspection of the file extension. This is obviously not sufficient all the time especially when programming for systems that may not use file extensions in the same way Windows does (e.g., Macs at least the older ones I have dealt with used resource and data forks and did not require an extension in the filename). However, we will stick with this example as it is very straight-forward.
Since Java is case-sensitive, for our simple test, we have to get lower case version of file name and check if it ends with desired extension (also in lower case).
As an alternative, we could also parse the file extension from f.getName(), store it in a variable called "fileext" for example, and then utilize the String object's equalsIgnoreCase() method.
Checkpoint: write your own program using JFileChooser and an anonymous implementation of FileFilter. An example is attached for your reference, but challenge yourself before taking a look.
Challenge: can you alter your program to only allow ".rtf" files that begin with the word "article"?
- 3
Custom File Filtering: Using Named FileFilter Implementation.
Let's take this a little further; we will now add more multiple options to the "Files of type:" drop-down list. Expanding on our simple anonymous class for filtering files based on the extension, we will now create a named implementation of javax.swing.filechooser.FileFilter. To keep things simple, the below is an example of a nested class that could reside in the same compilation unit we created above (e.g., see FileChooserExample.java in the Appendix A).
Extending FileFilter
As you can see, the logic of the FileFilter has been changed to be more dynamic/generic to allow for re-use; however, a lot of the basics hold true.
Using our Custom FileFilter
Now, let's use our named class, taking advantage of the re-usability by adding more than filter with differing number of extensions to the same file chooser:
That's it!
Run this code and you will see how the multiple filters are listed in the "Files of type:" drop-down list and how the files available for selection change as you choose different options from the drop-down.
Checkpoint: modify your program to use your own custom class that extends FileFilter, playing with more complex criteria. An example code snippet is below for your reference.
Challenge: can you alter your program to add a filter that will only accept PDF files smaller than 500 KB?
- 4
Wrapping Things Up.
For purposes of our test, save dialog was utilized but file dialogs can be used in other ways as well.
This will present a file dialog for an input file (to be opened):
Show a custom file dialog with approval command denoted by second parameter:
For all the file dialogs, we used a first parameter of null. This is the parent component of the file dialog; however, this can be null as shown if you just want to spawn the file dialog from an application that isn't all GUI based like we had above.
Moreover, there are easy ways to set the folder in which the file chooser starts your user when prompted to select file.
For this information and more complex/complete examples of using File Choosers in Java, see the reference link to online tutorial provided by Sun. You will find some additional references below that may be very useful, so please read and enjoy.
SUMMARY
In conclusion, file choosers are very useful to have in Swing/GUI based applications and even more so with the ability to customize the file filter(s) associated. The goal of this article was to demonstrate the ways of implementing this, so it is my sincere hope that it has been successful for that purpose and that you have fun coding away your own custom file filtering.
Lastly, special thanks to Venabili who graciously collaborated on the direction and wording of this article!
Happy coding!
Best regards,
Kevin (aka MWVisa1)
Appendix A: Anonymous File Filter Example.
Appendix B: Final Solution.
Downloads
Download JDK and/or NetBeans IDE
http://www.netbeans.org/do
or
http://www.sun.com/downloa
Download Eclipse IDE
http://www.eclipse.org/dow
Download TextPad
http://www.textpad.com/dow
Download NotePad++
http://notepad-plus.source
References:
How To Use File Choosers
http://java.sun.com/docs/b
Java Tip 85: Fun and games with JFileChooser
http://www.javaworld.com/j
Nested Classes
http://java.sun.com/docs/b