swing - Table Renderer not working correctly in Java -
i read lot of articles , got jtable rendering per requirements problem when table scrolled user, of other hidden parts of jtable colored shown in image below
and when user scrolls table, other parts got colored this
why happens? read many articles , and of them show same thing this...
here code
jtable table = new jtable() { public boolean iscelleditable(int rowindex, int colindex) { homecoming false; } }; (defaulttablecellrenderer)table.gettableheader().getdefaultrenderer()) .sethorizontalalignment(jlabel.center); table.setmodel(new defaulttablemodel(new object [][] {}, columns)); table.setrowheight(25); table.setautoresizemode(jtable.auto_resize_off); table.setselectionmode(listselectionmodel.single_selection); table.getcolumnmodel().removecolumn(table.getcolumnmodel().getcolumn(0)); table.setdefaultrenderer(object.class, new defaulttablecellrenderer() { @override public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { super.gettablecellrenderercomponent(table, value, isselected, hasfocus, row, column); if(!table.getmodel().getvalueat(row, 10).tostring().equals("true")) { setbackground(new color(246,137,137)); } homecoming this; } }); jscrollpane areascrollpane = new jscrollpane(table); areascrollpane.setpreferredsize(new dimension(width, height)); mypanel.add(areascrollpane);
all articles show way render custom row on status base. what's wrong in code? support.
if(!table.getmodel().getvalueat(row, 10).tostring().equals("true")) { setbackground(new color(246,137,137)); }
the same renderer used cells 1 time set background apply cells. so, think need like:
if(!table.getmodel().getvalueat(row, 10).tostring().equals("true")) { setbackground(new color(246,137,137)); } else setbackground( table.getbackground() );
you should add together code create sure cell not selected default selection color can painted.
instead of using custom renderer can override preparerenderer(...)
method of jtable. table row rendering show how can render row based on value in row.
java swing jtable rendering tablecellrenderer
No comments:
Post a Comment