簡體   English   中英

JTree:如何獲取節點的字體/文本顏色?

[英]JTree: how to get the Font/text color of the node?

我有以下代碼:

tree = getTreeComponent(BLA)

model = tree.getModel();
root = tree.getModel().getRoot();
childCount = tree.getModel().getChildCount(root);

childList = tree.getModel().getChild(root,x);

print childList

我可以將樹節點作為文本獲取,但不能將字體/文本的顏色節點獲取。

你能給我建議嗎?

節點的外觀與模型分開。 TreeCellRenderer負責呈現樹的節點,可以使用JTree.setCellRenderer()進行設置。

因此,如果您想知道節點的FontColor ,則應查閱JTree的渲染器。 例如:

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

渲染器的getTreeCellRendererComponent()返回一個Component ,該Component將用於繪制節點。

getTreeCellRendererComponent()的參數為:

JTree tree, Object value, boolean selected, boolean expanded,
boolean leaf, int row, boolean hasFocus

將不同的值傳遞給這些參數可能會導致返回的Component具有不同的Font和/或Color 指定有意義的參數值(例如,不應選擇它,因為它通常會顛倒顏色,它不應具有焦點,因為它也可能會改變顏色)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM