我疯了,因为我不明白我做错了什么。我需要我的树的每个节点都有一个不同的图标,就好像它是正确的、不正确的或需要更新一样。我看到了 Oracle 文档和很多网页(包括 Stackoverflow),但我的代码只显示了所有具有相同图标的节点。如果我做错了什么,我很抱歉,这是我的第一篇文章:>
显示: 我无法显示,因为我没有声望>.<
想象一棵树,并想象它总是显示带有gifNew图标的 leafIcon。似乎只有状态会监听最后一个节点。
这是代码(所有变量均已正确创建,像 gifNew 之类的图标也是如此):
图标:
static Icon closedIcon = new ImageIcon("folder.png");
static Icon openIcon = new ImageIcon("folder.png");
static Icon leafIcon = new ImageIcon("file.png");
static Icon gifWarn = new ImageIcon("warn.gif");
static Icon gifOk = new ImageIcon("ok.gif");
static Icon gifNew = new ImageIcon("plus.gif");
static Icon gifError = new ImageIcon("error.gif");
称呼:
tree.setCellRenderer(new TreeRenderer());
渲染器:
private static class TreeRenderer extends DefaultTreeCellRenderer {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean sel, boolean exp, boolean leaf, int row, boolean hasFocus) {
super.getTreeCellRendererComponent(tree, value, sel, exp, leaf, row, hasFocus);
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
String s = node.getUserObject().toString();
String error;
if (actualTree.equals("DOORS")){
error = checkTypeError(s, valuesClass.listOfValuesDOORS);
//System.out.println("DOORS - S: "+s);
}else{
error = checkTypeError(s, valuesClass.listOfValuesTC);
//System.out.println("TC - S: "+s);
}
switch (error) {
case "CORRECT":
setOpenIcon(openIcon);
setClosedIcon(closedIcon);
setLeafIcon(leafIcon);
break;
case "CREATE":
setOpenIcon(gifNew);
setClosedIcon(gifNew);
setLeafIcon(gifNew);
break;
case "DELETE":
setOpenIcon(gifError);
setClosedIcon(gifError);
setLeafIcon(gifError);
break;
case "UPDATE":
setOpenIcon(gifWarn);
setClosedIcon(gifWarn);
setLeafIcon(gifWarn);
break;
default:
setOpenIcon(openIcon);
setClosedIcon(closedIcon);
setLeafIcon(leafIcon);
//System.err.println("ERROR IN RENDERER. VALUE: "+error);
break;
}
return this;
}
/*****************************************
* Function that return which error have the actual node to push the icon when the tree is created.
*****************************************/
protected static String checkTypeError(String txt, List<valuesClass> list){
for (int i = 0; i < list.size(); i++) {
if (list.get(i).text.equals(txt))
if (list.get(i).create == true){
return "CREATE";
}else if (list.get(i).delete == true){
return "DELETE";
}else if (list.get(i).update == true){
return "UPDATE";
}else{
return "CORRECT";
}
}
return "DEFAULT";
}
}