-1

How to make the user to select the file from only specified folder

int returnVal = fc.showOpenDialog(FileChooser.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = fc.getSelectedFile();
    source = file.getAbsolutePath();
    fileName = file.getName();
    attachText.setText(fileName);
    source = source.replace("\\","\\\\");                
}

Here I will get the file from any folder, where I want the file only from G:\Project\Attachments. How Can i do this?

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
vijay
  • 1,129
  • 7
  • 22
  • 34

2 Answers2

2

You can pass the directory in the constructor:

JFileChooser filechooser = new JFileChooser(theDirectory);

or set it

filechooser.setCurrentDirectory(theDirectory);

in your case the directory is:

File theDirectory = new File("G:\\Project\\Attachments");
Alexi Akl
  • 1,934
  • 1
  • 21
  • 20
2
File dir = new File("G:\\Project\\Attachments");
FileSystemView fsv = new SingleRootFileSystemView(dir);
JFileChooser fileChooser = new JFileChooser(fsv);
int returnVal = fc.showOpenDialog(fileChooser);

if (returnVal == JFileChooser.APPROVE_OPTION) {
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101
  • 1
    +1, although SingleRootFileSystemView is not a standard class. Maybe you are referring to the class found in [Single Root File Chooser](http://tips4java.wordpress.com/2009/01/28/single-root-file-chooser/) – camickr Mar 14 '13 at 18:31