If it's not too late, this is what I have used, you can set the line fileChooser.setFileSelectionMode(DIRECTORIES); to filter for folders, or fileChooser.setFileSelectionMode(JFileChooser.FILE S_AND_DIRECTORIES); for both. Also notice that I filter for ".png" files only, so you have a choice there. I normally I put this in a small window with a frame and a panel with OK and Close Button, that way I have control over the cosmetics. In my case a click on OK, the window closes and returns a filename, or if you modify it the folder.
JFileChooser fileChooser = new JFileChooser(System.getProperty("user.dir"));
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".png");
}
@Override
public String getDescription() {
return "Directories and PNG files";
}
});
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (JFileChooser.APPROVE_SELECTION.equals(e.getActionCommand())) {
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null && selectedFile.isFile()) {
fileName = selectedFile.getAbsolutePath();
filenameField.setText(fileName);
if (!fileName.isEmpty() && new File(fileName).exists()) {
try {
// do whatever }
} else if (selectedFile != null && selectedFile.isDirectory()) {
fileChooser.setCurrentDirectory(selectedFile);
}
}
fileFrame.dispose();
}
});
hope that helps someone.