-1

如何使用户仅从指定文件夹中选择文件

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("\\","\\\\");                
}

在这里,我将从任何文件夹中获取文件,我只想从 G:\Project\Attachments 中获取文件。我怎样才能做到这一点?

4

2 回答 2

2

您可以在构造函数中传递目录:

JFileChooser filechooser = new JFileChooser(theDirectory);

或设置它

filechooser.setCurrentDirectory(theDirectory);

您的情况下,目录是:

File theDirectory = new File("G:\\Project\\Attachments");
于 2013-03-14T15:57:04.477 回答
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) {
于 2013-03-14T15:57:38.053 回答