When building a JTree it show paths for each node, any way to just show file/folder names?

283 views Asked by At

I've managed to get together a functional JTree that loads files and folders out from user.dir, but each node is named with the absolute path, which is kinda bothersome if you don't fancy node names that extends far beyond the borders. So I'm wondering if there is a simple solution to shorten the node names into, say parent directory and file/folder name?

Currently my nodes look like this:

/Users/username/documents/workspace/project/test.txt

What I'm looking for is more the lines of:

/test.txt

If this is something even a beginner should be shamefull for not comprehending, I expect some proverbial spanking. But if this qualifies as a decent question, feel free to twist your noodles. I tried looking into the java look and feel, but that turned out to be a dud.

I'm not sure if I need to write down my code. But if it's of any use, I've used a File array and .list() to get hold of the files, and then inserted 'em into the JTree with 'DefaultMutableTreeNode' and 'DefaultTreeModel'.

1

There are 1 answers

2
Andrew Thompson On BEST ANSWER

Use an appropriate TreeCellRenderer. See File Browser GUI for a great (IMO) example.