文件选择getAttachement只能从该文件夹

问题描述:

如何让用户从只有指定的文件夹文件选择getAttachement只能从该文件夹

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获取文件:\项目\附件。我怎样才能做到这一点?

+0

'文件选择.this'那是什么? – 2013-03-14 15:52:33

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) { 
+1

+1,但SingleRootFileSystemView不是标准类。也许你指的是[单根文件选择器]中找到的类(http://tips4java.wordpress.com/2009/01/28/single-root-file-chooser/) – camickr 2013-03-14 18:31:13

您可以通过目录在构造函数中:

JFileChooser filechooser = new JFileChooser(theDirectory); 

或将其设置

filechooser.setCurrentDirectory(theDirectory); 

你的情况目录为:

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

@vijay这将允许用户走出目录并选择其他文件。 – 2013-03-14 16:40:23