1

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

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
quidproquo
  • 41
  • 3
  • I posted something before an year, it might help you: http://stackoverflow.com/questions/12870908/how-to-add-checkbox-to-jtree-node-to-manage-multiselection/12870909#12870909 – Maxim Shoustin Dec 13 '13 at 22:42

1 Answers1

4

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

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • That's actually what I've based some of my code on. I forgot to mention that this is a school project, so using other people's work is frowned upon. – quidproquo Dec 13 '13 at 22:50
  • Sorry, that was just me being tiered of staring at the screen. I found exactly what I was looking for in the example you linked. Short, simple, and very much implementalbe (if that's even a word). Thanks for taking the time. – quidproquo Dec 13 '13 at 23:04