[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
JavaFAQ_JFileChooserCustomFilters

Code for File Filters in JFileChooser

I do not claim that this code is mine, but I have been givin it and I will publish it under the GPL License

This code, will create a file filter for you, you must only edit two areas. You will recognize which ones immediately. I will point it out nonetheless.

After creating one or two or as many filters as you want, you can run your JFileChooser, either OpenDialog or SaveDialog, and you will have filters.

loadPub.addChoosableFileFilter(new FileFilter() //adds new filter into list
    {
      String description = "Publisher Files (*.pbb)";
      /*change text within "" to text you want to appear in File Chooser*/
      
      String extension = "pbb";
      /*change text within "" for Filter you want created*/
      public String getDescription()
      {
        return description;
      }
      public boolean accept(File f)
      {
        if(f == null) return false;
        if(f.isDirectory()) return true;
        return f.getName().toLowerCase().endsWith(extension);
      }
    });
That's it, you need only change to your needs the commented text's respective code above. Then you must import the FileFilter package.

your import List should also have this: import javax.swing.filechooser.FileFilter;

Remember, if you want more than one filter, simply duplicate the above code, but changing the text within the " " to the new desired filter. If for anyreason this code is altered, please notify me, or place a comment about it being altered and why.

last edited (December 22, 2002) by KDivad Leahcim, Number of views: 1416, Current Rev: 1

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.