[英]JTree: how to get the Font/text color of the node?
I have the following code: 我有以下代码:
tree = getTreeComponent(BLA)
model = tree.getModel();
root = tree.getModel().getRoot();
childCount = tree.getModel().getChildCount(root);
childList = tree.getModel().getChild(root,x);
print childList
I can get the tree node as a Text but not the Color of Font/Text node. 我可以将树节点作为文本获取,但不能将字体/文本的颜色节点获取。
Could you give me suggestions? 你能给我建议吗?
The appearance of the nodes is separated from the model. 节点的外观与模型分开。 TreeCellRenderer
is responsible to render the nodes of the tree which can be set using JTree.setCellRenderer()
. TreeCellRenderer
负责呈现树的节点,可以使用JTree.setCellRenderer()
进行设置。
So if you want to know the Font
and Color
of a node, you should consult the renderer of the JTree
. 因此,如果您想知道节点的Font
和Color
,则应查阅JTree
的渲染器。 For example: 例如:
Component c = tree.getCellRenderer()
.getTreeCellRendererComponent(tree, node, false, false, false, 0, false);
Font font = c.getFont(); // Font used to render the node
Color color = c.getForeground(); // Foreground Color used to render the node
The renderer's getTreeCellRendererComponent()
returns a Component
which will be used to paint the node. 渲染器的getTreeCellRendererComponent()
返回一个Component
,该Component
将用于绘制节点。
The parameters of the getTreeCellRendererComponent()
are: getTreeCellRendererComponent()
的参数为:
JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus
Passing different values to these parameters may result in the returned Component
having different Font
and/or Color
. 将不同的值传递给这些参数可能会导致返回的Component
具有不同的Font
和/或Color
。 Specify meaningful parameter values (eg it should not be selected as it usually inverts the colors, it should not have focus as it might also change the color). 指定有意义的参数值(例如,不应选择它,因为它通常会颠倒颜色,它不应具有焦点,因为它也可能会改变颜色)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.