Solved

How to use setFilenameFilter method of FileDialog

Posted on 1997-03-10
1
1,672 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 47
Desingning Refactoring existing code 2 32
How do I remove an object from a 3 53
What's wrong with this code? 4 36
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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 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