Solved

How to use setFilenameFilter method of FileDialog

Posted on 1997-03-10
1
1,661 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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

772 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

10 Experts available now in Live!

Get 1:1 Help Now