java - TableCellRenderer - setRowHeight gone crazy -
i developing java swing application.
cell has ability wrap text , automatically increase height if needed wrap text.
i need setrowheight method me set row height. work fine before write setrowheight method application. after setrowheight method written, application overload , becomes heavy.
global variable processing
used count how many times texttablerenderer called. found out processing gives me value of thousand , more when application running.
why happen? have update runnable code on it. copy , paste , compile it. found out processing
keep increasing unstoppable.
import java.awt.color; import java.awt.component; import javax.swing.*; import javax.swing.border.matteborder; import javax.swing.table.tablecellrenderer; import javax.swing.table.tablecolumn; import javax.swing.text.view; public class sscce { public string[][] data = {{"a","b","c"},{"d","e","f"}}; public string[] column = {"1","2","3"}; private void displaygui(){ jtable table = new jtable(data,column); tablecolumn col; for(int = 0;i < column.length;i++){ col = (tablecolumn)table.getcolumnmodel().getcolumn(i); col.setcellrenderer(new texttablerenderer()); } jscrollpane scrollpane = new jscrollpane(table); jframe frame = new jframe("test"); frame.add(scrollpane); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(200,200); frame.setlocation(300,100); frame.setvisible(true); } public static void main(string[] args){ swingutilities.invokelater(new runnable() { public void run() { new sscce().displaygui(); } }); } public class texttablerenderer extends jtextarea implements tablecellrenderer { private int processing = 1; public texttablerenderer(){ setopaque(true); setlinewrap(true); setwrapstyleword(true); } public int getwrappedlines(jtextarea component) { view view = component.getui().getrootview(component).getview(0); int preferredheight = (int)view.getpreferredspan(view.y_axis); int lineheight = component.getfontmetrics( component.getfont() ).getheight(); return preferredheight / lineheight; } @override public component gettablecellrenderercomponent(jtable table, object value, boolean isselected, boolean hasfocus, int row, int column) { if (isselected) { if(hasfocus){ setborder(new matteborder(1,1,1,1,new color(122,138,153))); setbackground(table.getselectionbackground()); }else{ setborder(new matteborder(1,1,1,1,new color(184,207,229))); setbackground(table.getselectionbackground()); } } else { //setforeground(new color(80,90,155)); setborder(new matteborder(1,1,1,1,new color(255,255,255))); setbackground(table.getbackground()); } settext((value == null)? "" : value.tostring()); int h = getwrappedlines(this); table.setrowheight(row,h*18); system.out.println(" "+processing); processing ++; return this; } } }
Comments
Post a Comment