java - How to set custom icons for individual nodes on a JTree? -


i need able set icons jtree individual nodes. example, have jtree , need nodes have custom icons represent are.

  • (wrench icon) settings
  • (bug icon) debug
  • (smiley face icon) fun stuff

...

and on. have tried several sources , got 1 working, messed tree events so, no cigar. in advance.

as requested:

class country {     private string name;     private string flagicon;      country(string name, string flagicon) {         this.name = name;         this.flagicon = flagicon;     }      public string getname() {         return name;     }      public void setname(string name) {         this.name = name;     }      public string getflagicon() {         return flagicon;     }      public void setflagicon(string flagicon) {         this.flagicon = flagicon;     } }  class countrytreecellrenderer implements treecellrenderer {     private jlabel label;      countrytreecellrenderer() {         label = new jlabel();     }      public component gettreecellrenderercomponent(jtree tree, object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasfocus) {         object o = ((defaultmutabletreenode) value).getuserobject();         if (o instanceof country) {             country country = (country) o;             label.seticon(new imageicon(country.getflagicon()));             label.settext(country.getname());         } else {             label.seticon(null);             label.settext("" + value);         }         return label;     } } 

then it's initialized:

defaultmutabletreenode root = new defaultmutabletreenode("countries");     defaultmutabletreenode asia = new defaultmutabletreenode("general");     country[] countries = new country[]{             new country("properties", "src/biz/jabaar/lotus/sf/icons/page_white_edit.png"),             new country("network", "src/biz/jabaar/lotus/sf/icons/drive_network.png"),     };      (country country : countries) {         defaultmutabletreenode node = new defaultmutabletreenode(country);         asia.add(node);     } 

and works, it's don't want sub-roots show, nodes. also, code makes item won't highlight when click it.

i don't want sub-roots show, nodes.

your implementation of gettreecellrenderercomponent() should see conditioned boolean leaf parameter can use shown here.

if (o instanceof country && leaf) { ... } 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -