java - DefaultTreeCellRenderer not working as expected -


i'm being crazy because don't understand i'm doing wrong. need each node of tree have different icon if correct, incorrect or needs updating. saw oracle documentation , lot of webpages (including stackoverflow) code show nodes same icon. i'm sorry if did wrong, it's first post :>

display: can't show because no have reputation >.<

imagine tree , imagine show leaficon gifnew icon. seems if state listen last node.

here's code (all vars correctly created, icons gifnew too):

icons:

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"); 

call:

tree.setcellrenderer(new treerenderer()); 

renderer:

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 return error have actual node push icon when tree created.         *****************************************/         protected static string checktypeerror(string txt, list<valuesclass> list){              (int = 0; < 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";         }     } 

the behaviour describing caused fact not setting correctly node icon in switch statement. setopenicon(), setclosedicon() etc set icons renderer use whole tree. action taken in last node going decide icons rendered @ end.

the defaulttreecellrenderer extends jlabel. component used each node render contents. components' icon needs set i.e. code should this:

switch (error) {   case "correct":        seticon(leaficon); // sets icon of renderer jlabel        break;   case "create":        seticon(gifnew);        break;   case "delete":        seticon(giferror);        break;   ... } 

be sure set icon in cases though , make sure understand how renderer used render tree nodes (the same instance reused nodes)


Comments