Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Force JFileChooser to one directory and it's sub-folders

I have created a JFileChooser and I want to restrict it only in the user.home dir and it's subfolders.

The selection mode of my JFileChooser is directories only.

So far I have used this:

//JButton select = new JButton();
final File directorylock = new File(System.getProperty("user.home"));
JFileChooser browse = new JFileChooser(directorylock);
browse.setFileView(new FileView() {
    @Override
    public Boolean isTraversable(File f) {
         return directorylock.equals(f);
    }
});

But every time I open the JFileChooser it only shows me the user.home directory without it's subfolders and therefore I cannot access them or select them.

How it should work: Open JFileChooser and show the user.home directory with all it's subfolders. Be able to access the subfolders and select them. NOT be able to access parent folders. of the user.home directory.

I hope someone here knows how this is supposed to be done! :) Thank you guys in advance :D

like image 679
Andreas Neophytou Avatar asked Mar 13 '23 18:03

Andreas Neophytou


1 Answers

Check out the Single Root File Chooser.

You will only ever see the single file you specify in the combo box so you don't confuse the user that you can select a parent directory.

Then You will only be able to select a file in the directory or sub directory of the file you specify when creating the class.

like image 155
camickr Avatar answered Mar 23 '23 14:03

camickr