为 JTree 的 TreeNode 着色
我有一个 JTree,我给它实现了 TreeNode 接口的对象,还有一个自定义的 TreeModel 来显示它们(不使用 DefaultMutableTreeNode< /代码>)。我想更改某些节点的文本颜色。除了 javax.swing.tree.DefaultTreeCellRenderer.setTextNonSelectionColor(Color newColor) 之外,我在文档中找不到任何内容,但它会不加区别地更改所有内容,而且我只需要它用于某些节点(具体来说,损坏的链接,即在磁盘上找不到相应文件的节点,应显示为灰色,其余应为默认值)。能做到吗?如何做到?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
发布评论
评论(2)
您还可以查看 org.netbeans.swing.outline
,在此答案中提到。 TableCellRenderer
和 RenderDataProvider
接口使自定义树中行的外观变得特别容易。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
你已经接近你的答案了。您需要做的是将
DefaultTreeCellRenderer
子类化并重写DefaultTreeCellRenderer
的一些方法。然后确保告诉树使用自定义单元格渲染器。您需要做的是使用一些状态变量来指示链接是否已损坏,并据此设置节点的颜色。
You are close to your answer. What you need to do is Sub Class the
DefaultTreeCellRenderer
and override a few of theDefaultTreeCellRenderer
'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.