Guy Stimpson
Guy Stimpson

Reputation: 87

Recursively adding files to JList - only files in last folder are added

struggling again.

I'm recursively searching through a directory and picking out audio files by examining their extensions. Once found, each is to be added to a JList (in the main class - not shown here). However, only the last folder's files are being added to the list. Here is the code:

public void List(String path) throws InterruptedException, IOException {

        File root = new File(path);
        File[] list = root.listFiles();
        DefaultListModel lm = new DefaultListModel();

        if (list == null) {
            return;
        }

        for (File f : list) {
            if (f.isDirectory()) {
                List(f.getAbsolutePath());

            } else if (f.isFile()) {
                String outPath = f.getAbsolutePath();
                try {
                    String ext = outPath.substring(outPath.lastIndexOf(".") + 1);
                    if (ext.equals("wma") || ext.equals("m4a") || ext.equals("mp3")) {
                        lm.addElement(f.getAbsolutePath());
                    }
                } catch (Exception e) {
                    System.out.println(outPath + " is not a valid file!!!!!");
                }
                HomeScreen.Library.setModel(lm);

            }

        }

    }

I've tried replacing the lm.addElement(f.getAbsolutePath()) with a simple System.out.println(f.getAbsolutePath) and all files are printed out as expected. I've also tried moving HomeScreen.Library.setModel(lm); into different areas but that generally results in nothing being added to the list at all.

What I think must be happening is that each time a new folder is found, the list model is reset, somehow, and the files are added to the now empty model.

Is there a way around this? Am I doing something dopey in my code which is resulting in this apparent reset?

Many thanks in advance,

Guy

Upvotes: 1

Views: 143

Answers (2)

talex
talex

Reputation: 20474

You code create new model every tyme method invoked. You need create model once and update it.

My advice move DefaultListModel lm = new DefaultListModel(); out of you method and pass reference in it. Also move HomeScreen.Library.setModel(lm); out of you method and put it after method call.

DefaultListModel lm = new DefaultListModel();
some.List(path, lm);
HomeScreen.Library.setModel(lm);

PS: By java name convention method names starts from lover case letter.

Upvotes: 1

icza
icza

Reputation: 417877

The problem is that you create a new DefaultListModel in each call of your List() method. This explains why you think that

each time a new folder is found, the list model is reset, somehow, and the files are added to the now empty model

I think you want to add files to the same DefaultListModel so create it outside of the List() method, and either pass that as a parameter or make it an instance field which List() can access.

Upvotes: 1

Related Questions