扩展文件不显示在 JFileChooser 上

发布于 2024-10-25 06:53:02 字数 2569 浏览 3 评论 0原文

声明的扩展文件不会显示在 JFileChooser 窗口上。这是我的过滤器类:

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;
    }
}

这是我调用过滤器进程的代码:

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;
        }

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;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2024-11-01 06:53:02

我也测试过,效果很好。如果您仍然遇到问题,请发布您的完整代码。

在此处输入图像描述

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

enter image description here

意中人 2024-11-01 06:53:02

我测试了您的代码并修改了一些代码以进行文件过滤的简单测试(将选定的文件打印到标准输出),如下所示,它对于 AudioFilter 对象工作得很好...

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();
        }
    }

}

我想您需要检查您的 openFile() 方法的版本......特别是。你的 readFile(File) 方法

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文