swing - How to set color to a certain row if certain conditions are met using java? -
i have jtable. (tablesummary). 1 of it's column expiry. want highlight row client expiry date lapsed on current date..
i got logic can't make row turn red or other colors. here's code:
int count = (tablesummary.getrowcount()); numberformat formatter = new decimalformat("###,###"); string no = formatter.format(count); txtno.settext(no); simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); calendar cal = calendar.getinstance(); string expdatestring = sdf.format(cal.gettime()).tostring(); for(int i=0; i<=tablesummary.getrowcount()-1; i++){ string nnn= tablesummary.getmodel().getvalueat(i, 6).tostring(); system.out.println(nnn); int res = nnn.compareto(expdatestring); if(res>=0){ system.out.println("if ni " + (res>=0)); } else{ system.out.println("else ni" + (res>=0)); rowrenderer.setbackground(color.red); } }
can me this? because it's 1 of main highlight of interface. in advance !! :)
in following link, can find example of action want do: http://www.roseindia.net/java/example/java/swing/sadingrows.shtml
you have override preparerenderer()
on jtable
, add backgroundcolor on component
gets returned.
ps: future reference, easier if include more code. definition of rowrenderer
=)
edit
instead of normal jtable table = new jtable(model)
declaration, use following (change logic in preparerenderer
method if want else alternating color):
jtable table = new jtable(model) { public component preparerenderer(tablecellrenderer renderer, int index_row, int index_col) { // current row component comp = super.preparerenderer(renderer, index_row, index_col); // index, not selected if (index_row % 2 == 0 && !iscellselected(index_row, index_col)) { comp.setbackground(color.lightgray); } else { comp.setbackground(color.white); } return comp; } };
Comments
Post a Comment