Colouring JTree's TreeNodes
I have a JTree
which I give objects that implement the TreeNode
interface, and a custom TreeMod开发者_如何学Goel
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?
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.
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.
精彩评论