如何让JTree的节点根据标志的不同,显示不同的颜色

chlei 2004-09-27 10:36:23
每一个节点有一个标志位status,
当status='yes' 则显示红色
当status='no' 则显示绿色
...全文
96 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dlglmf 2004-09-27
  • 打赏
  • 举报
回复
已经说了,顶一下
shenyouth 2004-09-27
  • 打赏
  • 举报
回复
/**
* Implement the TreeCellRender,to change the display style.
*
* You can add the icon if you need.
*/
class CellRender extends JLabel
implements TreeCellRenderer {
protected Color selectedBackground;
protected Color selectedForeground;
protected Color noselectedBackground;
protected Color noselectedForeground;
protected Color overflowBackground = Color.yellow;
protected Color overflowForeground = Color.red;
protected Color overflowSelectedBG = Color.green;
protected Color overflowSelectedFG = Color.black;
public CellRender()
{
super();
}
/**
* Sets the value of the current tree cell to <code>value</code>.
*
* @return the <code>Component</code> that the renderer uses to draw
the value
* @param tree JTree
* @param value Object
* @param selected boolean
* @param expanded boolean
* @param leaf boolean
* @param row int
* @param hasFocus boolean
* @todo Implement this javax.swing.tree.TreeCellRenderer method
*/
public Component getTreeCellRendererComponent(JTree tree, Object
value,
boolean selected,
boolean expanded,
boolean leaf,
int row, boolean
hasFocus) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
NodeData nd ;//NodeData 是你自定义的包含“标记”属性的类
nd = (NodeData) node.getUserObject();
if (nd.标记.equals("yes")) {
if (selected)
{
//this.setForeground(Color.green);//你可以自己定义颜色,是定义鼠标点击之后的颜色
}
else
{
this.setForeground(Color.red);
}
}
else {
if(selected)
{
this.setForeground(Color.///);
}
else
{
this.setForeground(Color.green);
}
}

this.setText(value.toString());
return this;
}

}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧