Kristine Sarah Tan
Kristine Sarah Tan

Reputation: 99

Extension file doesn't show on JFileChooser

Declared extension files doesn't show on JFileChooser window. Here is my filter class:

import java.io.File;

public class AudioFilter extends javax.swing.filechooser.FileFilter{
    public boolean accept(File f){
        if (f.isDirectory()){
            return true;
        }
        String extension = Utils.getExtension(f);
        if (extension != null){
            if (extension.equals(Utils.wav)
            || extension.equals(Utils.aif)
            || extension.equals(Utils.rmf)
            || extension.equals(Utils.au)
            || extension.equals(Utils.mid)){
                return true;
            }else{
                return false;
            }
        }
        return false;
    }

    public String getDescription(){
        return "wav, aif, rmf, au, mid";
    }
}

class Utils{
    public final static String wav = "wav";
    public final static String aif = "aif";
    public final static String rmf = "rmf";
    public final static String au = "au";
    public final static String mid = "mid";
/*
* Get the extension of a file.
*/
    public static String getExtension(File f){
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');
        if (i > 0 && i < s.length() - 1){
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
}

Here is my code to call the filter process:

AudioFilter audiofiler = new AudioFilter();
    boolean openFile() throws FileNotFoundException{
            JFileChooser jfc = new JFileChooser();
            jfc.setDialogTitle("Open File");
            jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
            jfc.setCurrentDirectory(new File ("."));
            jfc.setFileFilter(audiofilter);
            int result = jfc.showOpenDialog(this);
            if(result == JFileChooser.CANCEL_OPTION){
                return true;
            }else if(result == JFileChooser.APPROVE_OPTION){
                fFile = jfc.getSelectedFile();
                String file_string = readFile (fFile);
                if(fFile.isDirectory()){
                    String[] filesInDirectory = fFile.list();
                    for(int i=0;i<filesInDirectory.length;i++){
                        jList1.setModel(list);
                        list.addElement(filesInDirectory[i]);
                    }
                }

                if(file_string != null){
                    fTextArea.setText(file_string);
                }else{
                    return false;
                }
            }
            return true;
        }

Upvotes: 0

Views: 1578

Answers (2)

eee
eee

Reputation: 1053

I tested your codes and modified a few for a simple test of file filtering (print out the selected file to std out) as follows and it works just fine for the AudioFilter object...

AudioFilter.java:

package file.test;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class AudioFilter extends FileFilter {

    @Override
    public boolean accept(File f) {
        if (f.isDirectory()){
            return true;
        }
        String extension = Utils.getExtension(f);
        if (extension != null){
            if (
                    (extension.equals(Utils.wav))
                ||  (extension.equals(Utils.aif))
                ||  (extension.equals(Utils.rmf))
                ||  (extension.equals(Utils.au))
                ||  (extension.equals(Utils.mid))
            ) {
                return true;
            }
            else {
                return false;
            }
        }
        return false;
    }

    @Override
    public String getDescription() {
        return "wav, aif, rmf, au, mid";
    }

    static class Utils{
        public final static String wav = "wav";
        public final static String aif = "aif";
        public final static String rmf = "rmf";
        public final static String au  = "au";
        public final static String mid = "mid";

        public static String getExtension(File f){

            String ext = null;
            String s = f.getName();
            int i = s.lastIndexOf('.');
            if (i > 0 && i < s.length() - 1){
                ext = s.substring(i+1).toLowerCase();
            }
            return ext;

        }
    }

}

AudioFilterTest.java:

package file.test;

import java.io.File;
import java.io.FileNotFoundException;

import javax.swing.JFileChooser;

public class AudioFilterTest {

    static AudioFilter audioFilter = new AudioFilter();

    public static boolean openFile() throws FileNotFoundException {
        JFileChooser jfc = new JFileChooser();

        jfc.setDialogTitle("Open File");
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setCurrentDirectory(new File("."));
        jfc.setFileFilter(audioFilter);
        int result = jfc.showOpenDialog(null);

        if (result == JFileChooser.CANCEL_OPTION){
            return true;
        }
        else if (result == JFileChooser.APPROVE_OPTION){
            File fFile = jfc.getSelectedFile();
            String filestr = fFile.getAbsolutePath();

            System.out.println(filestr);
        }

        return false;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            openFile();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

I guess you need to check your version of openFile() method...esp. your readFile(File) method

Upvotes: 0

o12
o12

Reputation: 465

I tested it too, it works fine. Post your full code, if you are still having problem.

enter image description here

Upvotes: 1

Related Questions