java - How to resize JTable column to string length? -


i have jtable have last column data field change different string values. want resize column string length. formula string length width?

i'm going using jtable.getcolumnmodel().getcolumn().setpreferredwidth() want know how translate string length width value.

you not interested in string length (nor mapping particular font/metrics). you're interested in preferredsize of renderingcomponent renderers cell content. that, loop through rows , query size, like

 int width = 0;  (row = 0; row < table.getrowcount(); row++) {      tablecellrenderer renderer = table.getcellrenderer(row, mycolumn);      component comp = table.preparerenderer(renderer, row, mycolumn);      width = math.max (comp.getpreferredsize().width, width);  } 

or use jxtable (in swingx project): has method pack() work :-)

edit: reason prefer table's preparerenderer on manually calling getxxrenderercomponent on renderer table might decorate visual properties of renderingcomponent. if decorations effect prefsize of component, manual config off.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -