1

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.)

Pshemo
  • 122,468
  • 25
  • 185
  • 269
Leftist Tachyon
  • 304
  • 1
  • 15
  • What do you mean by "similar system of selecting a folder that meet certain conditions"? Lets assume some of us don't use NetBeans so we don't know functionality you want to replicate. Can you describe it? – Pshemo Jan 04 '19 at 23:33
  • Use [edit] option (placed under tags) to clarify your question. – Pshemo Jan 04 '19 at 23:37
  • @Pshemo thanks for the suggestion! Please see the edits. – Leftist Tachyon Jan 04 '19 at 23:41
  • 1
    @LeftistTachyon What is your code currently doing instead? Which directories can you select which shouldn't be selectable and which directories can't you select which should be selectable? Please add an example of a directory structure you have on the computer you are testing, show the list of folders you want to be shown and show what you get instead. – Progman Jan 04 '19 at 23:53
  • @Progman please see the new edits. – Leftist Tachyon Jan 05 '19 at 00:04
  • @LeftistTachyon Do you have a `setFileFilter()` call in your code? It looks like this is required so the JFileChooser actually use your filter. – Progman Jan 05 '19 at 00:14
  • @LeftistTachyon Maybe you need to work around the `approveSelection()` method, see https://stackoverflow.com/questions/23092794/how-to-stop-jfilechooser-from-being-closed-when-approve-or-cancel-button-is-pres. You can use it to show a message like "Please select a netbeans project directory" but keep the selection dialog open. – Progman Jan 05 '19 at 00:16
  • @Progman Yes, I've added it in. – Leftist Tachyon Jan 05 '19 at 00:17
  • @LeftistTachyon Why do you have a different filters for `setFileFilter()` and `addChoosableFileFilter()`? If you have two different filters then.. well... you will have two different filters to choose from. – Progman Jan 05 '19 at 00:19
  • @Progman I guess I could do that, but it's not intuitive. If there is no "direct" solution, then that'll be what I do. :P – Leftist Tachyon Jan 05 '19 at 00:20
  • @Progman I could change that...but then even non-project folders will be shown and thus can be selected. One shows what I want to see (all folders), and the other one shows what I want to select (NB projects). – Leftist Tachyon Jan 05 '19 at 00:22
  • @LeftistTachyon Maybe you have to do something like on https://stackoverflow.com/questions/14061450/disable-open-button-in-jfilechooser – Progman Jan 05 '19 at 00:28
  • @Progman Hmmm. That seems like a OK solution. However, double-clicking a directory will still select it. – Leftist Tachyon Jan 05 '19 at 01:14

1 Answers1

0

It seems that there is no direct way or approach to solve this problem. I'll use my workaround instead, then.

The impetus for selecting the folder was to make serialization easier (one object per file), but I'll just stuff everything in one file instead.

Leftist Tachyon
  • 304
  • 1
  • 15