Reputation: 237
When I double click a directory JFileChooser is not opening that directory i.e. it's not browsing it but it selects the directory and returns.
How can I implement JFileChooser so that it will show the folder's content when I double click?
If setFileSelectionMode(JFileChooser.FILES_ONLY) is set then the behaviour is good as my needs but I have to use FILES_AND_DIRECTORIES.
Upvotes: 1
Views: 3958
Reputation: 5706
you can add your own MouseListener
final JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent arg0) {
if(arg0.getClickCount() == 2) {
File file = fileChooser.getSelectedFile();
if(file.isDirectory()) {
fileChooser.setCurrentDirectory(file);
fileChooser.rescanCurrentDirectory();
}
else {
fileChooser.approveSelection();
}
}
}
//Other methods (can be empty)
});
This checks for double clicks and gets the selected file from the JFileChooser
checks if that's a directory and if it is follows it, if it's a file it returns the file. Also if you select a directory and hit open it opens the directory.
Upvotes: 2