Solved

How to use setFilenameFilter method of FileDialog

Posted on 1997-03-10
1
1,677 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

717 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