简体   繁体   English

java jfilechooser只列出“hello * .txt文件”,没有目录更改

[英]java jfilechooser to list only “hello*.txt files” and no directory changing

I am trying to use JFileChooser to select files with this name format: LS48*.drv . 我正在尝试使用JFileChooser来选择具有此名称格式的文件: LS48 * .drv at the same time I want to limit the user to look into only a specific directory say c:\\data . 同时我想限制用户只查看一个特定的目录,比如c:\\ data So I don't want the user to be able to change directories or to other drive names. 所以我不希望用户能够更改目录或其他驱动器名称。 Base of my code segment below can you please provide me some hints: 我的代码段的基础可以请你提供一些提示:

 m_fileChooser = new JFileChooser("c:\\data"); // looking for LS48*.drv files
  m_fileChooser.setFileFilter(new FileNameExtensionFilter("drivers(*.drv, *.DRV)", "drv", "DRV"));

You will need to implement a FileFilter subclass of your own, and set this to the file chooser instead of a FileNameExtensionFilter instance. 您需要实现自己的FileFilter子类,并将其设置为文件选择器而不是FileNameExtensionFilter实例。

And your accept method in this subclass will be something like the following: 你在这个子类中的accept方法将类似于以下内容:

private static final Pattern LSDRV_PATTERN = Pattern.compile("LS48.*\\.drv");
public boolean accept(File f) {
    if (f.isDirectory()) {
        return false;
    }

    return LSDRV_PATTERN.matcher().matches(f.getName());

}

To prevent directory changes use this: 要防止目录更改,请使用:

File root = new File("c:\\data");
FileSystemView fsv = new SingleRootFileSystemView( root );
JFileChooser chooser = new JFileChooser(fsv);

Check this: http://tips4java.wordpress.com/2009/01/28/single-root-file-chooser/ 请查看: http//tips4java.wordpress.com/2009/01/28/single-root-file-chooser/

As for the file name pattern, you could use java regular expressions. 至于文件名模式,您可以使用java正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM