java - How do I use SwingX's Highlighter to return a different renderer component? -
i noticed swingx's highlighter
interface allows highlighter return different component 1 being passed in. can't find examples of being used, thought try use create kind of fake second column.
the intended result text in left column should truncate right column starts, can't use painter
. right column should render same width whole list, issue haven't figured out yet doesn't seem hard.
as right though, finding row height gets compressed small, can't see of text.
here's mean:
sample program:
import java.awt.borderlayout; import java.awt.component; import javax.swing.defaultlistmodel; import javax.swing.grouplayout; import javax.swing.jframe; import javax.swing.jscrollpane; import javax.swing.layoutstyle; import javax.swing.swingutilities; import org.jdesktop.swingx.jxlist; import org.jdesktop.swingx.decorator.abstracthighlighter; import org.jdesktop.swingx.decorator.componentadapter; import org.jdesktop.swingx.renderer.jrendererlabel; import org.jdesktop.swingx.renderer.stringvalue; public class renderertest implements runnable { public static void main(string[] args) { swingutilities.invokelater(new renderertest()); } @override public void run() { jframe frame = new jframe("highlighter test"); jxlist list = new jxlist(); defaultlistmodel<string> listmodel = new defaultlistmodel<>(); listmodel.addelement("one"); listmodel.addelement("two"); listmodel.addelement("three"); list.setmodel(listmodel); list.setvisiblerowcount(8); list.setprototypecellvalue("some string"); list.addhighlighter(new addsecondcolumnhighlighter(v -> ((string) v).touppercase())); jscrollpane listscroll = new jscrollpane(list); frame.setlayout(new borderlayout()); frame.add(listscroll, borderlayout.center); frame.pack(); frame.setvisible(true); } private static class addsecondcolumnhighlighter extends abstracthighlighter { private final stringvalue secondcolumnstringvalue; public addsecondcolumnhighlighter(stringvalue secondcolumnstringvalue) { this.secondcolumnstringvalue = secondcolumnstringvalue; } @override protected component dohighlight(component component, componentadapter adapter) { jrendererlabel rightcolumn = new jrendererlabel(); rightcolumn.settext(secondcolumnstringvalue.getstring(adapter.getvalue())); return new fixedsecondcolumnrendererlabel(component, rightcolumn); } } private static class fixedsecondcolumnrendererlabel extends jrendererlabel { private fixedsecondcolumnrendererlabel(component leadingcomponent, component trailingcomponent) { grouplayout layout = new grouplayout(this); setlayout(layout); layout.sethorizontalgroup(layout.createsequentialgroup() .addcomponent(leadingcomponent, grouplayout.default_size, grouplayout.default_size, short.max_value) .addpreferredgap(layoutstyle.componentplacement.related) .addcomponent(trailingcomponent)); layout.setverticalgroup(layout.createparallelgroup(grouplayout.alignment.baseline) .addcomponent(leadingcomponent) .addcomponent(trailingcomponent)); } } }
i'm wondering if there right way use bit of api. deliberately extended jrendererlabel
in case issue, seems more subtle...
if have @ jrendererlabel
, see invalidate
, revalidate
, validate
(and bunch of other methods) have been set "no operation", meaning no longer make notifications layout manager component should laid out. done improve performance of "stamping" renderer onto component (the jxlist
).
instead, use extend fixedsecondcolumnrendererlabel
jpanel
.
instead of creating new instance of fixedsecondcolumnrendererlabel
, jrendererlabel
each time method called, should consider optimising return same instance, 1 configured each time method called.
remember, method called each row in jxlist
, more have, more times called, more short lived objects create...
Comments
Post a Comment