I am working with the juce demo code to make a mp3 player.  I want to save the file selected and open to that place when started next time it is opened.  The code uses a FileTreeComponent, to display directoryList.  So after the FileTreeComponent is opened I try to use setSelectedFile to do this but does not work.  I must be misunderstanding. Here is a clipe of my code:

directoryList.setDirectory (g_Properties->getValue("PlayDirectory", ""), true, true);// set the directory last opened

thread.startThread (3);

fileTreeComp.setSelectedFile(g_Properties->getValue("LastPlayed", ""));


The play directory is "E:\\" and LastPlayed is somethinglike "E:\\album\\song.mp3"".  These were saved like this:

g_Properties->setValue("PlayDirectory", directoryList.getDirectory().getFullPathName());
g_Properties->setValue("LastPlayed", fileTreeComp.getSelectedFile().getFullPathName());


The setDirectory works correctly, the setSelectedFile does nothing.  Stepping through I get here:

void FileTreeComponent::setSelectedFile (const File& target)
    if (FileListTreeItem* t = dynamic_cast <FileListTreeItem*> (getRootItem()))
        if (! t->selectFile (target))

selectFile does this:

if (file == target)
            setSelected (true, true);
            return true;

 if (target.isAChildOf (file))
        { ...


        return false;


Of course my file does not match "" so it falls to the second if.  The next if calls:

bool File::isAChildOf (const File& potentialParent) const
    if (potentialParent.fullPath.isEmpty())
        return false;

This returns false. 

I assume that the getRootItem is the problem.  Do I need to setup the rootItem somehow or am I going about this in the wrong way?


I know the post was to long and maybe know wants to read the whole thing.  So let me phrase the question without all the details. 


I cannot get setSelectedFile to work with fileTreeComponent.  Either it does not do what I think it does, or I am doing something wrong.  Anyone know?


See previous post for details.





Hard to dig through your post and guess what you're doing, but obviously setSelectedFile does work, otherwise none of the file picker components would work. Maybe the file you're trying to select is invalid or not in the tree.


I'm sure it does work, but maybe I missunderstand what that is.  I want to programatically set the selected file to what was selected when the filetreecompont last closed.  So I get the selected file using getSelectedFile and save it on close.  Then when it is opened again and all is setup I do setSelectedFile with the file that was saved.  According to the debugger the file name being send to setSelectedFile is identical to the file name returned by getSelectedFile so the file name is valid.  My expectation is to see the tree view with the subdirectories open down to the selected file, and the file selected.  But it does nothing for me.

What should I expect to see in the displayed fileTreeView component after doing the getSelectedFile?






I decided to switch to a fileBrowserComponent with tree view.  This is working fine.  It does more of what I am am looking for anyway.