Tiago Martins
Tiago Martins

Reputation: 1

Java JFileChooser - I can choose a .txt only in a directory i set

Hi im working in a java project and i wanted to create a JFileChooser which i have but i want to choose only in one directory like i have the directory called "Saves" and i want the person who open the JFileChooser can only access txt inside the directory call "Saves". They cant acess anything else only the directory i set.

I wanted to only access txt and cant acess neither another directorys neither other paths

I researched a lot but i couldn't find anything. If you know about something else like another choose please help me! Thank you.

Upvotes: -2

Views: 76

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308259

A custom FileSystemView can be used to tweak what the user can see and where they can navigate. For example, this is a simple implementation that pretends that the passed-in directory is the root (and home) directory and has no parent (note that the majority of the methods just delegate to the system file system view):

public class RestrictedFileSystemView extends FileSystemView {
  private final FileSystemView delegate;
  private final File baseDirectory;

  RestrictedFileSystemView(File baseDirectory) {
    this.baseDirectory = baseDirectory;
    this.delegate = FileSystemView.getFileSystemView();
  }


  @Override
  public File[] getRoots() {
    return new File[] { baseDirectory };
  }

  @Override
  public File getHomeDirectory() {
    return baseDirectory;
  }

  @Override
  public File getDefaultDirectory() {
    return baseDirectory;
  }

  @Override
  public File getParentDirectory(File dir) {
    if (dir.equals(baseDirectory)) {
      return null;
    }
    return delegate.getParentDirectory(dir);
  }

  @Override
  public boolean isRoot(File f) {
    return f.equals(baseDirectory);
  }

  // all methods below here just delegate
  
  @Override
  public Boolean isTraversable(File f) {
    return delegate.isTraversable(f);
  }

  @Override
  public String getSystemDisplayName(File f) {
    return delegate.getSystemDisplayName(f);
  }

  @Override
  public String getSystemTypeDescription(File f) {
    return delegate.getSystemTypeDescription(f);
  }

  @Override
  public Icon getSystemIcon(File f) {
    return delegate.getSystemIcon(f);
  }

  @Override
  public Icon getSystemIcon(File f, int width, int height) {
    return delegate.getSystemIcon(f, width, height);
  }

  @Override
  public boolean isParent(File folder, File file) {
    return delegate.isParent(folder, file);
  }

  @Override
  public File getChild(File parent, String fileName) {
    return delegate.getChild(parent, fileName);
  }

  @Override
  public boolean isFileSystem(File f) {
    return delegate.isFileSystem(f);
  }

  @Override
  public boolean isHiddenFile(File f) {
    return delegate.isHiddenFile(f);
  }

  @Override
  public boolean isFileSystemRoot(File dir) {
    return delegate.isFileSystemRoot(dir);
  }

  @Override
  public boolean isDrive(File dir) {
    return delegate.isDrive(dir);
  }

  @Override
  public boolean isFloppyDrive(File dir) {
    return delegate.isFloppyDrive(dir);
  }

  @Override
  public boolean isComputerNode(File dir) {
    return delegate.isComputerNode(dir);
  }

  @Override
  public File createFileObject(File dir, String filename) {
    return delegate.createFileObject(dir, filename);
  }

  @Override
  public File createFileObject(String path) {
    return delegate.createFileObject(path);
  }

  @Override
  public File[] getFiles(File dir, boolean useFileHiding) {
    return delegate.getFiles(dir, useFileHiding);
  }

  @Override
  public File[] getChooserComboBoxFiles() {
    return delegate.getChooserComboBoxFiles();
  }

  @Override
  public boolean isLink(File file) {
    return delegate.isLink(file);
  }

  @Override
  public File getLinkLocation(File file) throws FileNotFoundException {
    return delegate.getLinkLocation(file);
  }

  @Override
  public File createNewFolder(File containingDir) throws IOException {
    return delegate.createNewFolder(containingDir);
  }
}

(Only tested quickly on Linux, other OS might require additional changes to some overrides).

Note that you can further restrict what directories the user can visit by overriding isTraversable() and even restrict what they see by overriding getFiles().

You can use this custom file system view in your JFileChooser like this:

FileSystemView fsv = new RestrictedFileSystemView(someBaseDirectory);
JFileChooser jfc = new JFileChooser(fsv);
jfc.showOpenDialog(parentComponent);

Upvotes: 5

Related Questions