Reputation: 289
Hy, i have a task to make, JFileChoser can get file from linux hard drive but he can show files and folders only in home directory. Can any one help?
Upvotes: 0
Views: 487
Reputation:
Not sure if it's possible to do what you want. One way of doing it would be to reject files selected which are not directly in the user's home directory, you could modify the accept method in this sample to return false if the select file is not $HOME + selected file
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
public class FileChooserDriver {
public static void main(String[] args) {
//get user home dir
String userHome = System.getProperty("user.home");
System.out.println("using user home -> " + userHome);
JFileChooser fileChooser = new JFileChooser(new File(userHome));
fileChooser.addChoosableFileFilter(new FileFilter() {
@Override
public String getDescription() {
return null;
}
@Override
public boolean accept(File f) {
System.out.println("accept called with -> " + f);
return false;
}
});
fileChooser.showOpenDialog(null);
File selectedFile = fileChooser.getSelectedFile();
System.out.println("selected -> " + selectedFile);
}
}
Upvotes: 1