当前位置: 技术问答>java相关
如何让jtree的叶子呈现不同的icon
来源: 互联网 发布时间:2015-06-25
本文导语: jtree的render只能让所有的叶子出现一种icon. | 建议从DefaultTreeCellRenderer继承自己的Renderer覆盖getTreeCellRendererComponent方法!这是我的一个列子 public MyRenderer() { greenIcon = new...
jtree的render只能让所有的叶子出现一种icon.
|
建议从DefaultTreeCellRenderer继承自己的Renderer覆盖getTreeCellRendererComponent方法!这是我的一个列子
public MyRenderer() {
greenIcon = new ImageIcon(getClass().getResource("/resource/green.jpg"));
redIcon = new ImageIcon(getClass().getResource("/resource/red.jpg"));
hostIcon = new ImageIcon(getClass().getResource("/resource/Host16.gif"));
}
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);
DefaultMutableTreeNode node=(DefaultMutableTreeNode)value;
Object someThing=node.getUserObject();
if(someThing instanceof ServiceHost){
setOpenIcon(hostIcon);
setClosedIcon(hostIcon);
}
else if(someThing instanceof ServiceConfig){
if (leaf && isRunning(someThing).compareTo("NO")==0) {
setIcon(redIcon);
}
else if(leaf && isRunning(someThing).compareTo("YES")==0){
setIcon(greenIcon);
}
else{}
}
else{
}
return this;
}
protected String isRunning(Object value) {
ServiceConfig nodeService=(ServiceConfig)value;
return nodeService.isRunning();
}
ServiceHost 和ServiceConfig
是我节点上的类
public MyRenderer() {
greenIcon = new ImageIcon(getClass().getResource("/resource/green.jpg"));
redIcon = new ImageIcon(getClass().getResource("/resource/red.jpg"));
hostIcon = new ImageIcon(getClass().getResource("/resource/Host16.gif"));
}
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);
DefaultMutableTreeNode node=(DefaultMutableTreeNode)value;
Object someThing=node.getUserObject();
if(someThing instanceof ServiceHost){
setOpenIcon(hostIcon);
setClosedIcon(hostIcon);
}
else if(someThing instanceof ServiceConfig){
if (leaf && isRunning(someThing).compareTo("NO")==0) {
setIcon(redIcon);
}
else if(leaf && isRunning(someThing).compareTo("YES")==0){
setIcon(greenIcon);
}
else{}
}
else{
}
return this;
}
protected String isRunning(Object value) {
ServiceConfig nodeService=(ServiceConfig)value;
return nodeService.isRunning();
}
ServiceHost 和ServiceConfig
是我节点上的类
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。