aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes custom icons in JTree Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "custom icons in JTree" Watch "custom icons in JTree" New topic
Author

custom icons in JTree

Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
OK, I'm stumped
trying to use custom icons for the nodes in a JTree
I made my own TreeCellRenderer



and I used it like this when I create and populate my tree



I run the code in a debugger and everything seems to work right but on the GUI there are just default icons
I even made a JLabel and set the icon to display in it and that works so the image is getting loaded
I'm guessing there's something wrong with the order in which I'm doing things, like when stuff get's created but it looks right to me
I can put a breakpoint in MyTreeCellRenderer and see the code jump in there

any ideas???

Dave
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18902
    
    8

Do your tree nodes REALLY contain DefaultMutableTreeNode objects as their user objects? If not, then your renderer would be throwing a class-cast exception where you cast the value to that type. And if you do, that's a really strange design.
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
my nodes are objects that extend DefaultMutableTreeNodes
they're extended such that they contain info about how to lookup stuff for that node in a database
the functions I'm calling, set*Icon haven't been overloaded so I think what I'm doing is ok, but I could be wrong
I have been before

Dave
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
hmm, Paul
I think you're onto something
investigating further
thanks

Dave
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
doh
what's behind door number 3
it would be a "really strange design" :roll:
I shouldn't extend DefaultMutableTree node like I'm doing
I should use setUserObject to include the additional info
my bad

Thanks
Dave
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
Dave Robbins
Ranch Hand

Joined: Sep 16, 2003
Posts: 131
ok, found the problem
the folks who think IDE's are terrible and you should write all your code with a text editor are gonna laugh
after I built my nice little tree with all the custom icons, the auto-generated code that netbeans had written came along behind me and re-initialized my tree
interestingly, now that I've found the problem I see that creating my own treenodes that extend a DefaultMutableTreeNode isn't a problem
I didn't think it should be
but now that I see how I can use setUserObject to do the same thing I see that it's kinda a waste of time

Thanks for all the help
Dave
 
 
subject: custom icons in JTree