java - What is causing arbitrary JTree Nodes to incorrectly display their names and how can I stop it from happening? -



java - What is causing arbitrary JTree Nodes to incorrectly display their names and how can I stop it from happening? -

i have constructed rather big jtree xml data, literally thousands of nodes, of display correctly. reason node's userdata strings not beingness displayed, rather thay cutting short or cutting off ... appended end of name.

the affected nodes appear random, , different each time tree refreshed or recreated xml.

things i've ruled out:

not plenty space display total name.

the jtree within jscrollpane plenty of horizontal space, jscrollpane isn't showing horizontal scroll bar indicate lack of space.

even shortest name can affected.

the cutting off point isn't consistent shortened node names.

incomplete name loaded xml

if affected node isn't leaf , expanded or collapsed, total name displayed when re-rendered, xml isn't consulted @ during process. edit:

as requested explanation of code behind jtree:

the creation of treemodel , population of tree:

public xmldialogtree(document doc) { defaulttreemodel treemodel = new defaulttreemodel(buildtreenode(doc.getelementsbytagname("dialogs").item(0))); setmodel(treemodel); } // recursive function build tree private defaultmutabletreenode buildtreenode(node xmlnode) { // create sure node's name description of is, opposed generic xml tag xmldialogtreenode = new xmldialogtreenode(xmlnode.getattributes().getnameditem("name").getnodevalue()); treenode.controlname = xmlnode.getnodename(); // add together children treenode based on xmlnode's children nodelist nodelist = xmlnode.getchildnodes(); (int = 0; < nodelist.getlength(); i++) { node tempnode = nodelist.item(i); if (tempnode.getnodetype() == node.element_node) { // loop 1 time again if has kid nodes treenode.add(buildtreenode(tempnode)); } } homecoming treenode; }

where doc org.w3c.dom.document containing parsed xml , xmldialogtreenode literally javax.swing.tree.defaultmutabletreenode extended contain string controlname = "customnodename"

the custom cellrenderer follows, code pretty much loading custom icons, nil should impact displayed name.

public class xmldialogtreecellrenderer extends defaulttreecellrenderer { private static final long serialversionuid = 1l; // icons used in jtree, loaded 1 time on needed basis once, , stored here. private static final map<string, icon> icons = new hashmap<string, icon>(); static { // create sure default icon loaded loadicon("_not_found"); } @override public component gettreecellrenderercomponent(jtree tree, object value, boolean sel, boolean exp, boolean leaf, int row, boolean hasfocus) { xmldialogtreenode node = (xmldialogtreenode) value; seticons(node.controlname); super.gettreecellrenderercomponent(tree, value, sel, exp, false/* leaf */, row, hasfocus); homecoming this; } /** * assigns both open , close icons, specific control. loads them if necessary. * * @param controlname * - name of command in xml , name of image in resources.treeicons package. */ private final void seticons(string controlname) { // create sure node has controlname set if (controlname != null) { // seek , pre-loaded icon icon controlicon = icons.get(controlname); // wasn't there, seek loading if (controlicon == null) loadicon(controlname); // if icon doesn't exist, set default code safe setopenicon(controlicon); setclosedicon(controlicon); // stop here don't set default icons 1 time again return; } setopenicon(getdefaultopenicon()); setclosedicon(getdefaultclosedicon()); } /** * * * @param iconname * @return */ private static final void loadicon(string iconname) { url url = xmldialogtreecellrenderer.class.getresource("/com/phabrix/resources/dialogtreeicons/" + iconname + ".png"); if (url == null) { // tell developer need create new icon new command type if (main.debug && !iconname.equals("dialogs")) system.out.println("there no icon command named: " + iconname); url = xmldialogtreecellrenderer.class.getresource("/com/phabrix/resources/dialogtreeicons/_not_found.png"); } icons.put(iconname, new imageicon(toolkit.getdefaulttoolkit().getimage(url))); } }

so simple answer; code:

// seek , pre-loaded icon icon controlicon = icons.get(controlname); // wasn't there, seek loading if (controlicon == null) loadicon(controlname);

became code:

// seek , pre-loaded icon icon controlicon = icons.get(controlname); // wasn't there, seek loading if (controlicon == null) { loadicon(controlname); controlicon = icons.get(controlname); }

note upon checking see if our icon variable null, , making sure icon has been loaded program, create sure icon variable updated no longer null.

java jtree

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -