Link to home
Start Free TrialLog in
Avatar of tegz
tegz

asked on

auto resize jtable column width

i have a jtable and i want to be able to auto resize the width of the columns to fit the data.
how do i do this?
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Avatar of tegz
tegz

ASKER

that doesnt resize according to the what i have stored in a particular column
The only way I have seen this done is to get the maximum length string in the column (by iteration and comparison) and use FontMetrics to calculate its size, then set the col width to that size.
Avatar of tegz

ASKER

how do i get the maximum length string in a columns though?
any examples?
>>
how do i get the maximum length string in a columns though?
any examples?
>>

Scan the table model and:

http://javaalmanac.com/egs/java.awt/TextDim.html
Avatar of tegz

ASKER

for (int i = 0; i < tb.getColumnCount(); i++)
 {
//get data length, how?
//set column prefered size?
}

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tegz

ASKER

i applied: http://javaalmanac.com/egs/javax.swing.table/PackCol.html

didnt change the code, it works all aprt from one long text field.
any ideas why?