Solved

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

Posted on 2004-08-18
8
514 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
  • 4
8 Comments
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
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
Comment Utility

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
Comment Utility
try using setFileFilter()
0
 
LVL 15

Expert Comment

by:Javatm
Comment Utility
> setFileFilter()

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

Expert Comment

by:Javatm
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now