Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java: FileFilter

javax.swing.filechooser.FileFilter is used to restrict the files that are shown in a JFileChooser. By default, a file chooser shows all user files and directories in a file chooser dialog, with the exception of "hidden" files in Unix (those starting with a '.'). You may restrict the list that is shown by setting the file filter for a file chooser dialog.

Name Confusion. There are several file filtering classes and interfaces in Java, which often leads to confusion.

  • javax.swing.filechooser.FileFilter - Use with JFileChooser. An abstract class that you must extend, defining accept(f) and getDescription().
  • java.io.FileFilter - Pass to the File listFiles(ff) method. An interface for which you must define accept(f).
  • java.io.FilenameFilter - Similar to java.io.FileFilter, in that your can call the File list(fnf) method, but apparently restricts the selection further to one name. Implementing this interface requires defining accept(dir, name). Doesn't seem to be as generally useful.

Setting a FileFilter

You need to create a file filter object by subclassing the javax.swing.filechooser.FileFilter class and defining the two methods accept and getDescription.

import javax.swing.*;
. . .
JFileChooser fc = new JFileChooser();
FileFilter filter = new FileFilter() {
        .
fc.setFileFilter(FileFilter filter);

To display only specified extensions

This example displays only .html files. Note that it's very important to also allow directories (f.isDirectory()) if you want the user to be able to move around the file system.

// File :   fileutilstest/Test.java
// Purpose: Restricts JFileChooser to show only  HTML files.
// Author:  Fred Swartz
// Date:    2005-02-25

import java.io.*;

class HTMLFileFilter extends javax.swing.filechooser.FileFilter {
    public boolean accept(File f) {
        return f.isDirectory() || f.getName().toLowerCase().endsWith(".html");
    }
    
    public String getDescription() {
        return ".html files";
    }
}

For this to work, be sure that the file chooser looks at both files and directories.

fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

Setting a list of user selectable file filters

In an output file dialog, the user is sometimes presented with a choices of the format to save a file. These choosable file filters can be set and examined with:

fc.addChoosableFileFilter(FileFilter filter);
filter = fc.getFileFilter();

Don't use ...

You will not use java.io.FileFilter or java.io.FilenameFilter for a file chooser dialog - they have other uses.

Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (post your own) View All Comments Latest 10 Comments:

java code for packet filtering

Posted by kanagaraj on Saturday, 03.21.09 @ 08:26am | #86087

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]