I have a JTree
which I give objects that implement the TreeNode
interface, and a custom TreeModel
to display them (not using DefaultMutableTreeNode
). I would like to change the text colour of some nodes. I can't find anything in the docs, except javax.swing.tree.DefaultTreeCellRenderer.setTextNonSelectionColor(Color newColor)
, but it will change everything indiscriminately, and I only need it for some nodes (specifically, broken links, i.e. nodes whose corresponding files can't be found on the disk, should be greyed out, the rest should be default). Can it be done, and how?
views:
54answers:
2
+2
A:
You are close to your answer. What you need to do is Sub Class the DefaultTreeCellRenderer
and override a few of the DefaultTreeCellRenderer
's methods. Then make sure you tell the tree to use your custom cell renderer.
What you will need to do is have some state variables that indicate whether or not a link is broken, and set the color of the node based on that.
jjnguy
2010-05-24 04:11:29
Obviously, state variables were not a problem :), but purely `JTree`'s interface... Anyway, did you mean ` getTreeCellRendererComponent`? Is it as simple as just returning a JLabel from that function? Will try when I get a bit of free time, in couple of hours. Thanks...
Amadan
2010-05-24 04:38:02
A:
You might also look at org.netbeans.swing.outline
, mentioned in this answer. Ordinary extensions of TableCellRenderer
and the RenderDataProvider
interface make it especially easy to customize the appearance of rows in the tree.
trashgod
2010-05-24 16:06:41