Building the Image Name Filter
File:
ImageNameFilter.java
Contents
Overview
The ImageNameFilter class is designed to filter files presented in a dialog. A filter takes a group of files and determines if they meet a certain criteria. If a file adheres to the specifications, it is displayed in the dialog. Files that dont meet these criteria are filtered and not displayed in the file list of the dialog. Our ImageNameFileFilter
filters files based on whether or not they appear to be image files (which is based on the extension of the filename).
Our class implements the FilenameFilter interface, which specifies that derived classes implement a single accept( ) method.
Steps to Follow
When a filename filter is used by a file dialog, the accept( ) method of the filter is called once per file in the file list. It is the job of the method to either accept or reject the file by returning a Boolean. The file returns true if it meets the criteria of the filter and should be displayed or false otherwise.
import java.io.File;
import java.io.FilenameFilter;
public class ImageNameFilter implements FilenameFilter
{
/**
* Tests if a specified file should be included in a file
* list.
*
* @param dir the directory in which the file was found.
* @param name the name of the file.
* @return true if the name should be included in the file
* list; false otherwise.
* @since JDK1.0
*/
public Boolean accept(File dir, String name)
{
|
//Need to filter for image files (files whose names
//end with ".jpg", ".gif", or ".jpeg", regardless
//of case).
//Insert "ImageNameFilter accept"
|
Note that we import both java.io.File and java.io.FileNameFilter. As we previously stated, our class implements the FilenameFilter interface, and we implement the single method accept( ). This method returns a Boolean
and takes both a file reference and string (the filename). These references are passed to us automatically by the FileDialog we are registered with.
Locate the ImageNameFilter
accept clipping in the ImageNameFilter folder and drag it directly
below the last line of code shown above. Your code should now look like
this:
import java.io.File;
import java.io.FilenameFilter;
public class ImageNameFilter implements FilenameFilter
{
/**
* Tests if a specified file should be included in a file
* list.
*
* @param dir the directory in which the file was found.
* @param name the name of the file.
* @return true if the name should be included in the file
* list; false otherwise.
* @since JDK1.0
*/
public Boolean accept(File dir, String name)
{ //Need to filter for image files (files whose names
//end with ".jpg", ".gif", or ".jpeg", regardless
//of case).
//Insert "ImageNameFilter accept
|
String upperCaseName = name.toUpperCase( );
return (upperCaseName.endsWith(".JPG") ||
upperCaseName.endsWith(".JPEG") ||
upperCaseName.endsWith(".gif"));
|
}
}
|
Our code for determining whether the file is an image or not is very simplistic. We take the file name, and we convert it to uppercase characters, storing the result in a temporary variable. Then we return the result of the Boolean expression that returns true if the name string ends with ".jpg", ".jpeg", or ".gif". This is a simple way of checking, but it wont work if the extension of the file is incorrect or absent.
FileName Filters are very simple classes. They implement an interface which contains a single method named accept( ). The method takes a ile reference and a string reference (the name of the file), and the filter can check these objects against some criteria and accept or reject the file based on the return value of the method.
Our filter simply checks the end of the file name to see if it matches a specific extension.
This concludes the source file modification for our tutorial. Click here
to return to the main tutorial file where we will put together the finishing
pieces needed to build our application.
|