I could use some help. I've got a JTree, it's a file browser. I'm trying to programatically select a cell in the tree. I'm making the following calls:

File home = new File(System.getProperty(HOME_DIR));
setTree(home, newFile); 
TreePath path = this.pathFromFile(home);
tree.setSelectionPath(path);

But, that doesn't seem to highlight the cell. I've checked, and the path is valid, the path is getting expanded, but the folder isn't highlighted as if someone had clicked on it - which is what I'm going for.

Anyone have any ideas.

Thanks in advance, Bryant P.

Are you wanting to highlight the parent of the file node or the file node itself? Your code should be selecting (and highlighting) the last node of the path you specified. Are you certain the path extends all the way to the node you are wanting to select and isn't the path to the parent component?

The home directory variable is "user.home", which, in my case comes to "C:\Documents and Settings\bpurdin". I'd like that folder to be highlighted - as if someone had already clicked on it, so that it stands out more. Is there a way to do that?

Thanks, Bryant P.

Expanding that path and selecting it should do it if that is the node that you are building the TreePath from.

Well, that's the issue I'm having. The path is being expanded, but my question is how to progrmatically select the folder node in the tree. The code I posted earlier doesn't do that, and it looks like it should. Do you know of any call I'm missing.

Thanks, Bryant P.

Not really. If "path" terminates at that directory node, then setSelectionPath(path); should leave it selected. Are you certain that your TreePath actually ends at that node, not above or below it? Is nothing else resetting or clearing the selection path? Those are the only things I can think to check.

Thanks. That's what I thought too. I just spent some time looking around, and I didn't see anything. I tried moving the selection call to the end of the constructor, but that didn't work. Oh well, I guess I'll just make a note of it to one day play around with it again - if there's time.

Thanks for your help.

- Bryant P.

Thanks. That's what I thought too. I just spent some time looking around, and I didn't see anything. I tried moving the selection call to the end of the constructor, but that didn't work. Oh well, I guess I'll just make a note of it to one day play around with it again - if there's time.

Thanks for your help.

- Bryant P.

Ah, you have the call in the constructor? I was fiddling with some demo code to verify my suggestions and I did have some problems with selection behavior when the call was in the constructor - it just would not work correctly. When I moved that same code to a button it worked just fine. You may want to try moving the selection code to another method that executes after the component is fully constructed and rendered.

Thanks for looking into this. I moved the call outside to another method, and I stil couldn't get it to work. I've got the tree expanded to that directory, and I guess I'll just leave it at that for now. Maybe I'll look into it again down the road.

Thanks, Bryant P.

This article has been dead for over six months. Start a new discussion instead.