Limiting Viewable Directories with FileDialog

I am writing an application that needs to allow the user to create lists of files (specifically images) by choosing them one at a time from various locations on another computer on the network. Is there any way I can force a FileDialog to stay within a certain directory? (Example: I create a FileDialog, but I only want it to be able to browse through C:\My Stuff\Images\* it should not be able to access anything higher than C:\My Stuff\Images\. Also, how can I set it to look for specific file types ( jpg, tga, gif, ... )?
Cmerf486Asked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
why don't you just get the list of files to choose from and display then in a JList or similiar, then you have full controil.
0
 
CEHJCommented:
You need to override FileView.isTraversable
0
 
CEHJCommented:
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
objectsCommented:
try this:

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;


public class Chooser {

      public static void main(String[] args) {
            File dir = new File(args[0]);
            JFileChooser chooser = new JFileChooser();
            chooser.setCurrentDirectory(dir);
            chooser.setFileFilter(new FileFilter() {

                  @Override
                  public boolean accept(File f) {
                        String name = f.getName();
                        return f.isFile() && (name.endsWith(".gif") || name.endsWith(".jpg"));
                  }

                  @Override
                  public String getDescription() {
                        return "image files";
                  }});
            chooser.setAcceptAllFileFilterUsed(false);
            chooser.showOpenDialog(null);
      }
}



Specify the directory to display when running it, eg.

java Chooser /objects/images
0
 
Cmerf486Author Commented:
Okay, I see now hos to keep a user from going down further into the directory tree, but how do I keep them from going up?
0
 
objectsCommented:
don't believe you can.
0
 
Cmerf486Author Commented:
I was thinking about that (and I will probably end up doing that since I can't limit directory traversal), but for strictly aesthetic reasons, it was preferred to do it the other way. Oh well, thanks objects!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.