如何更改单个JTree节点的样式(颜色,字体) [英] How to change style (color, font) of a single JTree node
问题描述
我在JFrame
的两个面板中有两个JTree
.我想更改从一棵树拖放到另一棵树上的节点的样式(颜色和字体).请提供一种永久更改JTree
节点颜色的方法.
I have two JTree
in two panels in a JFrame
. I want to change the style(color and font) of nodes on drag and drop from one tree to the other.Please provide me a way to change the color of a JTree
node permanently.
推荐答案
首先,您将需要一个可以处理样式和颜色的数据对象.您可以将DefaultMutableTreeNode子类化,并使用getts和setters添加这些数据项
To start, you will need to have a data object that can handle style and color. You could subclass DefaultMutableTreeNode and add these data items with getts and setters
然后,您需要创建一个自定义TreeCellRenderer.我建议扩展DefaultTreeCellRenderer,并在重写的处理程序中,检查您的自定义类,并修改JLabel输出以使用Font和Color(如果设置了这些值)
Then you'd need to create a custom TreeCellRenderer. I recommend extending DefaultTreeCellRenderer, and in the overridden handler, checking for your custom class, and modifying the JLabel output to use the Font and Color if these values are set
这篇关于如何更改单个JTree节点的样式(颜色,字体)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!