?
Solved

JFilechooser to allow user only to select from 1 folder only

Posted on 2003-02-23
7
Medium Priority
?
1,697 Views
Last Modified: 2007-12-19


JFileChooser FileC = new JFileChooser("C:\messy");
      int result = FileC.showOpenDialog(this);
      if( result == JFileChooser.CANCEL_OPTION )
      {
        return;
      }


I have it starting from the folder C:\messy but currently a user can go to all directories from this starting position.
0
Comment
Question by:bananaDick
[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
7 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 8005798
You need to define a file filter to filter out all directories:

class MyFileFilter extends javax.swing.filechooser.FileFilter {

  public boolean accept(File file) {
    return file.isFile();
  }

  public String getDescription() {
    return "Files Only";
  }
}

JFileChooser FileC = new JFileChooser("C:\messy");
FileC.setFileFilter(new MyFileFilter());
int result = FileC.showOpenDialog(this);
if( result == JFileChooser.CANCEL_OPTION )
{
  return;
} else {

  // get selected file.
  File selectedFile = FileC.getSelectedFile();

  // make sure it's really a file and not directory.
  if (selectedFile.isFile()) {
    // it's a file.
  }
}

0
 

Author Comment

by:bananaDick
ID: 8009088
I just want it to choose from that given folder. It has to show all files within that folder. i.e. C:\messy is a folder.
0
 
LVL 5

Expert Comment

by:vemul
ID: 8009209
I don't think it is possible to do that
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:bananaDick
ID: 8009221
thats pretty disappointing. It seems like a pretty obvious feature to have.
0
 
LVL 9

Accepted Solution

by:
yongsing earned 320 total points
ID: 8013269
Alternatively, you can create a JDialog that shows only all files in that folder. Then the user will have no way of going to other folders. Use the File.listFiles() method to get all the files in that folder.
0
 

Expert Comment

by:CleanupPing
ID: 9059130
bananaDick:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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 …
Suggested Courses
Course of the Month10 days, 19 hours left to enroll

770 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