Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java File class help...

Options
  • 04-03-2003 12:44pm
    #1
    Posts: 0


    hey all,

    sorry if this was asked before, I haven't seen it up before.

    I have the user selecting a directory through a JFileChooser and, from this, I am displaying the list of files within that directory.

    I want to filter the files of a specific type, so that erroneous files aren't processed, but when I use the method in File class,

    file.listFiles(new CustomFileFilter());

    it won't compile,saying it doesn't recognise the method which takes a file filter as an argument. The only way to use the fileFilter is through extending it or implementing it, and that doesn't work, which is driving me up the wall.

    I just want the list of files to be of a specific type, any help would be great on this, thanks :)


Comments

  • Registered Users Posts: 660 ✭✭✭anthonymcg


    Had this working on a college project before but can't remember the exact code. Will look it up tonight and get back to ya but you might solve it in the meantime.


  • Registered Users Posts: 6,240 ✭✭✭hussey


    You need to extend FileFilter

    eg.
    public class ImageFilter extends FileFilter {
    
        //Accept all directories and all gif, jpg, tiff, or png files.
        public boolean accept(File f) {
            if (f.isDirectory()) {
                return true;
            }
    
            String extension = Utils.getExtension(f);
            if (extension != null) {
                if (extension.equals(Utils.tiff) ||
                    extension.equals(Utils.tif) ||
                    extension.equals(Utils.gif) ||
                    extension.equals(Utils.jpeg) ||
                    extension.equals(Utils.jpg) ||
                    extension.equals(Utils.png)) {
                        return true;
                } else {
                    return false;
                }
            }
    
            return false;
        }
    
        //The description of this filter
        public String getDescription() {
            return "Just Images";
        }
    }
    
    to set the view try 
    
                private JFileChooser fc;
    
                fc = new JFileChooser();
    
                //Add a custom file filter and disable the default
                //(Accept All) file filter.
                fc.addChoosableFileFilter(new ImageFilter());
    
    

    HERE is a tutorial on FileChoosers/fiters

    Note you will find the class Utils at the above location


  • Posts: 0 [Deleted User]


    Cheers lads, thanks :)


Advertisement