Solved

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

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

622 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