Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Custom Jtree renderer  RSS feed

 
Gabriel Kecske
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys!

I would like to ask your help about JTree custom renderer. I've been trying to figure out what could be wrong with this. Here's the deal:

I have a Jtree which suppose to view the structure of given folder, so far so good its working. But my problem is that i dont want it to show the full path as name, only folder name and file name. I wrote a custom JTree renderer in hope that this will solve my problem but it didnt.

Here is my custom Jtree renderer:



and here is where Im trying to implement it:



I have no idea what could be wrong
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if value is an instanceof DefaultMutableTreeNode, it can't ever be an instanceof File, so the inner if condition will never be met.

To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.

And welcome to the Ranch!
 
Gabriel Kecske
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the response Darryl. I really dont know how the posted code could be more SSCCE compatible, it has only that code that i have problem with it. I didnt posted the all 1200+ lines. By the way I tried to remove the outer condition , but its still not working.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing value on lines 12-13 into userValue.
 
Gabriel Kecske
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Try changing value on lines 12-13 into userValue.


Thank you Rob for taking your time to answer me. I tried to change those as you suggested but still not working. I'm starting to wonder if its even possible to show only names not full path :-/
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure it is. Your renderer just needs to do the correct thing.

Let's start with your model. I assume you have a DefaultTreeModel with DefaultMutableTreeNodes, and those DefaultMutableTreeNodes contain Files. Is this correct? Can you validate this by printing out the values?
You should now be able to see what the values actually are. Perhaps they are not what you want.

What I usually do when creating renderers (TableCellRenderer, ListCellRenderer or TreeCellRenderer) is call super.getXxxRendererComponent at the end, with a possibly other value. For instance:
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might find it a lot easier for your DefaultMutableTreeNode to just override toString() to return whatever you want to see in the tree node and you don't need to mess about with cell renderers. Something like this:



You'll probably need a suitable constructor as well but that's the part which answers your question.
 
Gabriel Kecske
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Sure it is. Your renderer just needs to do the correct thing.

Let's start with your model. I assume you have a DefaultTreeModel with DefaultMutableTreeNodes, and those DefaultMutableTreeNodes contain Files. Is this correct? Can you validate this by printing out the values?
You should now be able to see what the values actually are. Perhaps they are not what you want.

What I usually do when creating renderers (TableCellRenderer, ListCellRenderer or TreeCellRenderer) is call super.getXxxRendererComponent at the end, with a possibly other value. For instance:


Hi Rob!

Thank you for the answer yes it does print out the node values as i excepted. I have one more question, is it enough to initialize a custom renderer like:
or I have to do anything else? Because I think this renderer now should work, it should make those changes that i want but it doesnt
 
Gabriel Kecske
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:You might find it a lot easier for your DefaultMutableTreeNode to just override toString() to return whatever you want to see in the tree node and you don't need to mess about with cell renderers. Something like this:



You'll probably need a suitable constructor as well but that's the part which answers your question.


Hey Paul :) Thank you for the answer. I tried your solution, and i have one question. How come if FileTreeNode extends DefaultMutableTreeNode it doesnt recognize node object? Or i miss something? it says: "node cannot be resolved"
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because I wrote that code quickly, copying and pasting data from your original code, and didn't test it. There's a simple fix:

 
Gabriel Kecske
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, could you please guide me how can i make of use that code, how can i implement it to my jtree? Thank you very much in advance!

BTW I use this to populate the jtree with data:



and im using it like this, where dir is a path:

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At line 31:

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!