Solved

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

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

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 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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

777 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