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
Post a Comment