Solved

can't figure how to restrict JfileChooser to show only one directory should be using JDialog?

Posted on 2004-08-18
8
519 Views
Last Modified: 2008-01-09
Hi Experts,

I have not been able to figure how to restrict JFileChooser to only let me look at ONE directory. I have scanned through the anwers in the JFileChooser section, although, I didn't see a jfilechooser answer, I did see one thought that I hadn't considered, use JDialog to show files in a single directory and let my user choose from this view. I have worked at that JDialog solution but just can't figure it out. Does anyone know how to emulate JFileCHooser's ability to select a file from a directory by using JDialog? Maybe someone knows how to restrict JFileChooser to only look at one directory and NOT let the user change to another directory??  Either one, its all the same to me.

Sincerely,

Steve  
0
Comment
Question by:stevehiwy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
8 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 11836266
You can have it open in a specific folder but I dont think you can retrict it from opening any other folder, you can though filter the files.
0
 
LVL 15

Accepted Solution

by:
Javatm earned 125 total points
ID: 11836288

JFileChooser x = new JFileChooser();

// This is the selection mode . . .
x.setFileSelectionMode(JFileChooser.FILES_ONLY);
// Specifieng the directory . . .
x.setCurrentDirectory(new File("C:/YourFolder"));

Hope that helps . . .
Javatm
0
 
LVL 92

Expert Comment

by:objects
ID: 11836459
try using setFileFilter()
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11836695
> setFileFilter()

Is just for filtering the file not the folder.
0
 
LVL 15

Expert Comment

by:Javatm
ID: 11836726
If you decided to just filter the file then you can create a custom filter like this :

/**
 * Filter1 Class
 *
 * Author : Javatm
 * Date    : 08/18/2004
 * Time   : 7:27 PM
 *
 */

import java.io.*;

public class Filter1 extends javax.swing.filechooser.FileFilter {

  public boolean accept (File file) {

  return file.isDirectory() || file.getAbsolutePath().endsWith(".txt");
  }

  public String getDescription () {

  return "Text files  \"*. txt\"";
  }
}

And call it in you JFileChooser like :

JFileChooser x = new JFileChooser();

x.addChoosableFileFilter(new Filter1());
x.setFileSelectionMode(JFileChooser.FILES_ONLY);
x.setCurrentDirectory(new File("C:/YourFolder"));

Hope that helps . . .
Javatm
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 contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

752 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