FileChooser仅从此文件夹获取getAttachement

时间:2013-03-14 15:50:46

标签: java swing jfilechooser

如何让用户从指定的文件夹中选择文件

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。我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

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

JFileChooser filechooser = new JFileChooser(theDirectory);

或设置

filechooser.setCurrentDirectory(theDirectory);

你的情况目录是:

File theDirectory = new File("G:\\Project\\Attachments");

答案 1 :(得分: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) {