• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1696
  • Last Modified:

How to use setFilenameFilter method of FileDialog

I need to know how to use the setFilenameFilter method of FileDialog. I want to open a FileDialog with only files ending with ".txt" to be listed.
The method requires an argument of type FilenameFilter, but I am not sure how to create and use a FilenameFilter.
Sample code of how this mechanism works would be helpful.
0
andrewmears
Asked:
andrewmears
1 Solution
 
amarshalCommented:
The FilenameFilter is ment as an interface to allow filtering by other classes.  There are not any given filter, nor are there ways to acquire them.  You must subclass the FilenameFilter yourself, which gives you a great amount of flexability with what criteria a filter uses.

Here is a sample filter for what you were talking about (this is on the fly, not debugged; you'll have to double check it):

// Filename Filter for
class TextFileFilter implements java.io.FilenameFilter   {
 // default constructors

   public boolean accept( File dir, String name )   {
      return name.endsWith(".txt");
      }
   }

Then create an instance to pass to the FileDialog:
   ...
   file_dialog.setFilenameFilter( new TextFileFilter() );
   ...

If you do this often, it's a good idea to make a single generalized filter to use.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now