极端基本的JFileChooser问题

极端基本的JFileChooser问题

本文介绍了Java-极端基本的JFileChooser问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个JFileChooser,它允许用户选择任何包含单词"hello"的文件或目录.也就是说,以下是有效的选择:

I'd like to make a JFileChooser that lets the user select any file OR directory that contains the word "hello". That is, the following are valid selections:

C:\hello\
C:\fun\hello.txt
etc.

我该如何工作?我尝试过:

How can i get this working? I've tried:

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;

        JFileChooser f = new JFileChooser();
        f.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        f.setFileFilter(new FileFilter() {

            public boolean accept(File file) {
                return file.getAbsolutePath().contains("hello");
            }

            public String getDescription() {
                return "hi";
            }

        });
        f.showOpenDialog(this);

但是我的JFileChooser是空白的.那就是说,我的C:\中根本看不到文件夹"fun".

but my JFileChooser is blank. That is to say, I can't see folder "fun" in my C:\ at all.

推荐答案

我不得不做一些其他的覆盖.

I had to do some additional overriding.

两个自定义类和一个跑步者类.试试看.

Two custom classes and a runner class. Give 'em a try.

package custom_file_chooser;

import java.io.File;

import javax.swing.filechooser.FileFilter;

public class MyFileFilter extends FileFilter {

    private String filterString;

    public MyFileFilter(String filterString) {
        super();
        this.filterString = filterString;
    }

    @Override
    public boolean accept(File file) {
         return  file.isDirectory() || file.getName().toLowerCase().contains(filterString);
     }

     @Override
     public String getDescription() {
         return filterString + " only";
     }
}


package custom_file_chooser;

import java.io.File;

import javax.swing.JFileChooser;

public class MyFileChooser extends JFileChooser {

    private String filterString;

    public MyFileChooser(String filterString) {
        super();
        this.filterString = filterString;

        setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        setFileFilter(new MyFileFilter(filterString));
    }

    @Override
    public void approveSelection() {
        File file = this.getSelectedFile();
        if (!file.isDirectory() || file.getName().contains(filterString)) {
            super.approveSelection();
        }
    }
}


package custom_file_chooser;

import java.io.File;

import javax.swing.JFileChooser;

public class CustomFileChooserRunner {


    public static void main(String[] args) {

        MyFileChooser f = new MyFileChooser("hello");
        int result;

        result = f.showOpenDialog(null);

        if (result == JFileChooser.APPROVE_OPTION) {
            File file = f.getSelectedFile();
            System.out.println("You chose to open: " +
                    file.getName() + (file.isDirectory() ? " directory" : " file"));
        }
    }
}

这篇关于Java-极端基本的JFileChooser问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 19:07