Currently I'm attempting to replicate NetBeans' "Open Project" file choosing dialog because I would like a similar system of selecting a folder that meet certain conditions. However, I'm having trouble forcing the dialog to choose those that meet my condition and not any folder. How could I go about doing this?
EDIT: the condition needed to be met in order for a folder to be considered a NetBeans Project is that it needs to contain a nbproject
folder.
So far, I've tried using .setFileSelectionMode
, but that's not restrictive enough for my purposes.
My code (in main
method):
JFileChooser chooser = new JFileChooser();
FileFilter filter = new FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
// check if this folder is a NetBeans Project
File[] list = f.listFiles();
for (File sub : list) {
if (sub.isDirectory()) {
if (sub.getName().equals("nbproject")) {
return true;
}
}
}
return false;
} else {
return false;
}
}
@Override
public String getDescription() {
return "NetBeans Projects";
}
};
// so I can select the folder...
// ...but I only want the user to be able to select stuff my filter accepts
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.addChoosableFileFilter(filter);
chooser.setFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
@Override
public String getDescription() {
return "Folders";
}
});
chooser.setAcceptAllFileFilterUsed(false);
chooser.showOpenDialog(null);
ADDENDUM: Here's a example file structure that I would have to navigate:
C:\USER\Documents\NetBeansProjects
|- Project
|- nbproject
|- New Folder (empty)
I want to only be able to select the Project
folder and not New Folder
. Both still need to be visible. However, when I select New Folder
, the program approves and closes the window.
This is not the behavior I want. Instead, I want the window to simply open the folder.
(sidenote: I don't really want to rewrite an entire class just for this purpose. But if it does come down to that, I could use a workaround that won't need this to happen.)