java - JTree - Move nodes to the right -
i have jtree several node levels. changed icons root nodes bigger ones. problem is, icons bigger default ones, vertical dashed lines , corresponding child nodes not centered below root node icon.
see following picture:
my question is: possible move except root nodes little bit right side?
set left margin in jtree , draw real root node icon separately in jlayer.
import java.awt.*; import javax.swing.*; import javax.swing.plaf.*; import javax.swing.tree.*; public class middlexofrootnodeicontest { public jcomponent makeui() { int ow = uimanager.geticon("tree.openicon").geticonwidth(); int iw = 32; icon icon1 = new coloricon(color.green, new dimension(ow, iw)); icon icon2 = new coloricon(new color(0x550000aa, true), new dimension(iw, iw)); jtree tree = new jtree(); tree.setborder(borderfactory.createemptyborder(1, 1 + iw / 2, 1, 1)); //test: //tree.setborder(borderfactory.creatematteborder(1, 1 + iw / 2, 1, 1, color.red)); tree.setcellrenderer(new compoundtreecellrenderer(icon1, iw)); layerui<jtree> layerui = new layerui<jtree>() { @override public void paint(graphics g, jcomponent c) { super.paint(g, c); graphics2d g2 = (graphics2d) g.create(); icon2.painticon(c, g2, 1 + ow / 2, 1); g2.dispose(); } }; jpanel p = new jpanel(new gridlayout(1, 3)); p.add(new jscrollpane(new jtree())); p.add(new jscrollpane(new jlayer<jtree>(tree, layerui))); return p; } public static void main(string... args) { eventqueue.invokelater(() -> { jframe f = new jframe(); f.setdefaultcloseoperation(windowconstants.exit_on_close); f.getcontentpane().add(new middlexofrootnodeicontest().makeui()); f.setsize(320, 240); f.setlocationrelativeto(null); f.setvisible(true); }); } } class compoundtreecellrenderer extends defaulttreecellrenderer { private static final color alpha_zero = new color(0x0, true); private final jpanel p = new jpanel(new gridbaglayout()); private final gridbagconstraints ci = new gridbagconstraints(); private final gridbagconstraints cl = new gridbagconstraints(); private final jlabel icon = new jlabel(); private final icon gap1 = new coloricon(alpha_zero, new dimension(2, 10)); private final icon gap2; private final icon rooticon; public compoundtreecellrenderer(icon rooticon, int iw) { super(); dimension d = new dimension(2 + (iw - rooticon.geticonwidth()) / 2, 10); this.rooticon = rooticon; //test: this.gap2 = new coloricon(color.blue, d); this.gap2 = new coloricon(alpha_zero, d); this.setopaque(false); p.setopaque(false); ci.gridx = 0; ci.weightx = 1.0; cl.gridx = 1; cl.anchor = gridbagconstraints.west; } @override public component gettreecellrenderercomponent( jtree tree, object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasfocus) { jlabel l = (jlabel) super.gettreecellrenderercomponent( tree, value, selected, expanded, leaf, row, hasfocus); l.setopaque(false); p.removeall(); if (value.equals(tree.getmodel().getroot())) { icon.seticon(rooticon); l.seticon(gap2); } else { icon.seticon(l.geticon()); l.seticon(gap1); } p.add(icon, ci); p.add(l, cl); return p; } } class coloricon implements icon { private final color color; private final dimension dim; public coloricon(color color, dimension dim) { this.color = color; this.dim = dim; } @override public void painticon(component c, graphics g, int x, int y) { graphics2d g2 = (graphics2d) g.create(); g2.translate(x, y); g2.setcolor(color); g2.fillrect(1, 1, dim.width - 2, dim.height - 2); g2.dispose(); } @override public int geticonwidth() { return dim.width; } @override public int geticonheight() { return dim.height; } } 

Comments
Post a Comment