Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Using java.io.FilenameFilter

Posted on 1997-12-24
9
Medium Priority
?
294 Views
Last Modified: 2012-08-14
I would like to know how to use java.io.FilenameFilter with a FileDialog class.  I only want my FileDialog class to accept files of type .exe.  How do I do this?
0
Comment
Question by:bjg
[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
9 Comments
 

Author Comment

by:bjg
ID: 1231824
Also how do I tell it to start in the Windows directory?
0
 
LVL 16

Expert Comment

by:imladris
ID: 1231825
A filename filter looks something like this:

class exeFilt implements FilenameFilter
{      public boolean accept(File dir,String nm)
      {      return(nm.endsWith(".exe"));
      }
}

You attach it to the FileDialog with the setFilter method:

FileDialog fd=new FileDialog.....
fd.setFilenameFilter(new exeFilt());


0
 
LVL 16

Expert Comment

by:imladris
ID: 1231826
And as to the second question, FileDialog provides:

    public void setDirectory(String dir) {

which you would give the relevant argument (e.g. "c:\\windows")


0
Independent Software Vendors: 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!

 

Author Comment

by:bjg
ID: 1231827
1)  I have tried the exact code you have here for using FilenameFilter and when I run my app I get the following System.out.println:  setFilenameFilter not implemented

2)  I have tried setDirectory("C:\\Windows") and it still defaults to the current directory which I am running the app from.

What is wrong with these two things?
0
 

Author Comment

by:bjg
ID: 1231828
I got setDirectory to work and I used setFile("*.exe") to only allow me to accept files of type .exe in the FileDialog.  I still do not know what is wrong with the FilenameFilter.
0
 
LVL 16

Expert Comment

by:imladris
ID: 1231829
Can't imagine either. If you have the source you can see it right in the FileDialog class, so it should not be able to come up with that particular error. It would not imply that there is anything wrong with the filter. "setFilenameFilter not implemented" hints more at something like a misspelling, miscapitalization of the method or the class. Though, obviously, everything looks fine in your comments here.
0
 
LVL 1

Expert Comment

by:cu
ID: 1231830
Here is a codelet that implements FilenameFilter and it works:

public class extFilter implements FilenameFilter
{
  private String ext;
  private boolean acceptDir = true;

  public extFilter(String extension){
    this.ext = ext;
  }

  public extFilter(String ext, boolean acceptDir){
    this.ext = ext;
    this.acceptDir = acceptDir;
  }

  public boolean accept(File dir, String name)
  {
    if (name.endsWith(ext))
      return true;
    else
      return ((acceptDir) && (new File(dir,name)).isDirectory());
  }
}

0
 

Author Comment

by:bjg
ID: 1231831
this is the code that I have and it didn't work:

class MyFilenameFilter implements FilenameFilter {
    public boolean accept(File dir,String name) {
        return(name.endsWith(".exe"));
    }
}

Should this work if I only want the FileDialog to show and allow files of type .exe?
0
 
LVL 4

Accepted Solution

by:
russgold earned 200 total points
ID: 1231832
Try this:

class MyFilenameFilter implements FilenameFilter {
    public boolean accept(File dir,String name) {
        return(name.toUpperCase().endsWith(".EXE"));
        }
    }

Note that Windows is case-insensitive, and by default its 8.3 names are returned in upper case.  You need to convert everything to a single case to be certain of finding them.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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:
Suggested Courses

618 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