Solved

How to use setFilenameFilter method of FileDialog

Posted on 1997-03-10
1
1,663 Views
Last Modified: 2008-03-10
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
Comment
Question by:andrewmears
1 Comment
 

Accepted Solution

by:
amarshal earned 50 total points
ID: 1218989
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now