Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Changing Font of DefaultMutableTreeNode

197 views
Skip to first unread message

Daniel Fabian

unread,
Mar 27, 2004, 3:57:43 AM3/27/04
to
Hi!

I have a treenode and want to change the color and font of a treenode. I
have already implemented a TreeCellRenderer, but whenever I use that to
change the color of the font, the font of the whole tree changes. All I want
is to change a single TreeNode. What am I doing wrong?

private class LinkTreeCellRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
row, hasFocus);
Object nodevalue = null;
if (!(value instanceof DefaultMutableTreeNode))
return this;
nodevalue = ((DefaultMutableTreeNode)value).getUserObject();
if ((nodevalue instanceof Page) && ((Page)nodevalue.someattr)) {
setFont(this.getFont().deriveFont(Font.BOLD));
setTextNonSelectionColor(Color.BLUE);
}
return this;
}
}

Thanks in advance,
Daniel


Babu Kalakrishnan

unread,
Mar 27, 2004, 4:37:47 AM3/27/04
to
Daniel Fabian wrote:
>
> I have a treenode and want to change the color and font of a treenode. I
> have already implemented a TreeCellRenderer, but whenever I use that to
> change the color of the font, the font of the whole tree changes. All I want
> is to change a single TreeNode. What am I doing wrong?
>

The same renderer is used to render all the nodes. So you need to reset
the values when it is called with other nodes as the parameter.

> private class LinkTreeCellRenderer extends DefaultTreeCellRenderer {

Font normalFont,boldFont;

public LinkTreeCellRenderer()
{
super();
normalFont = getFont();
boldFont = normalFont.deriveFont(Font.BOLD);
}

> public Component getTreeCellRendererComponent(JTree tree, Object value,
> boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
> super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf,
> row, hasFocus);
> Object nodevalue = null;
> if (!(value instanceof DefaultMutableTreeNode))
> return this;
> nodevalue = ((DefaultMutableTreeNode)value).getUserObject();
> if ((nodevalue instanceof Page) && ((Page)nodevalue.someattr)) {

//setFont(this.getFont().deriveFont(Font.BOLD));
setFont(boldFont);

> setTextNonSelectionColor(Color.BLUE);
> }
else
{
setFont(normalFont);
setTextNonSelectionColor(Color.BLACK); // Or whatever
}

> return this;
> }
> }
>

Daniel Fabian

unread,
Mar 27, 2004, 5:13:49 AM3/27/04
to
"Babu Kalakrishnan" wrote...

> The same renderer is used to render all the nodes. So you need to reset
> the values when it is called with other nodes as the parameter.

Works perfect! Thanks alot!

Greetings,
Daniel

0 new messages