Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colouring JTree's TreeNodes

Tags:

java

swing

jtree

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?

like image 571
Amadan Avatar asked May 22 '26 20:05

Amadan


1 Answers

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.

like image 126
jjnguy Avatar answered May 24 '26 10:05

jjnguy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!