Solved

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

Posted on 2004-08-18
8
518 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Suggested Solutions

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…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

756 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