[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Limiting Viewable Directories with FileDialog

Posted on 2007-10-16
7
Medium Priority
?
236 Views
Last Modified: 2013-12-29
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, ... )?
0
Comment
Question by:Cmerf486
  • 3
  • 2
  • 2
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 20088460
You need to override FileView.isTraversable
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20089101
0
 
LVL 92

Expert Comment

by:objects
ID: 20090340
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Cmerf486
ID: 20093590
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
 
LVL 92

Expert Comment

by:objects
ID: 20093614
don't believe you can.
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 20093688
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
 

Author Comment

by:Cmerf486
ID: 20094909
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question