FileChooser仅从此文件夹获取getAttachement

时间:2022-12-30 00:30:47

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?

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

2 个解决方案

#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) {

#2


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");

#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) {

#2


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");