Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java File class help...

  • 04-03-2003 12:44PM
    #1
    Posts: 431 ✭✭


    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, Registered Users 2 Posts: 663 ✭✭✭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, Registered Users 2 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: 431 ✭✭ [Deleted User]


    Cheers lads, thanks :)


Advertisement