0

Possible Duplicate: How do I restrict JFileChooser to a directory?

    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.

Community
  • 1
  • 1
nasser
  • 79
  • 1
  • 4
  • 1
    And before someone else votes to close this, I would like to point out that the solution posted here is slightly different than the other solution presented and the poster may (or may not) prefer the solution presented here. – camickr Jun 04 '11 at 20:15
  • 1
    @camickr: Why not copy your solution [here](http://stackoverflow.com/questions/32529/how-do-i-restrict-jfilechooser-to-a-directory) and vote to delete this question? – trashgod Jun 05 '11 at 01:31
  • 1
    @trashgod, because the other question is 3 years old. If someone does a search and finds the question, they will see an accepted answer and an answer with 10 up votes. Chances are they will not notice that a new answer has been added (who looks a the date an answer is posted?) and therefore ignore the answer since it has no upvotes. People in this forum are too quick to close postings and not allow new solutions and ideas. – camickr Jun 05 '11 at 02:52
  • @camickr:Good points. Also, merely copying an answer fails to preserve the existing value of that answer. IIUC, the preferred approach is to flag the question for moderator attention, as discussed [here](http://meta.stackexchange.com/questions/7270). I defer to you on whether to do so. Good article, BTW. – trashgod Jun 05 '11 at 12:25
  • @trashgod, the problem is people are so inconsistent. The majority of questions posted are "duplicates" of other questions. Why is this question not closed: http://stackoverflow.com/questions/6243086/java-swing-how-to-scroll-down-a-jtextarea? Hovercraft answered that question even though he voted to close this question. I guess if people know the answer they answer the question so they can get "reputation points". If they don't know the answer they close it? – camickr Jun 05 '11 at 15:56
  • @camickr: Indeed, people are inconsistent, yet there is remarkable consensus here, too. As to motive, I can only answer for myself: I like points; I like good answers. I am inclined to preserve your good answer, but only with your consent. – trashgod Jun 05 '11 at 18:17

1 Answers1

4

Single Root File Chooser limits the slection to a single directory and its children.

If you want to prevent the selection of child directories then you would also need to add a FileFilter:

chooser.removeChoosableFileFilter( chooser.getAcceptAllFileFilter() );
chooser.addChoosableFileFilter( new FileFilter()
{
    public boolean accept(File f)
    {
        return ! f.isDirectory();
    }

    public String getDescription()
    {
        return "Files only";
    }
});
camickr
  • 321,443
  • 19
  • 166
  • 288