Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1700
  • Last Modified:

JFilechooser to allow user only to select from 1 folder only



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
bananaDick
Asked:
bananaDick
1 Solution
 
yongsingCommented:
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
 
bananaDickAuthor Commented:
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
 
vemulCommented:
I don't think it is possible to do that
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
bananaDickAuthor Commented:
thats pretty disappointing. It seems like a pretty obvious feature to have.
0
 
yongsingCommented:
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
 
CleanupPingCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now