| Author |
jtable column size
|
sudheer kiran
Ranch Hand
Joined: Jun 26, 2008
Posts: 237
|
|
hi all
i have a jtable,i want to have all columns Width to be the size of contents in rows.
if anything added in row then the column width should increase to fit that value.
any suggestion please.
|
Sudheer
SCWCD, SCJP 5
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
TableColumn has methods for setting the preferred, minimum and maximum widths. That leaves calculating the sizes based on the contents, and that's where the renderer can help you out.
Loop through the table, get a renderer and editor for each cell, get its component and get its preferred size:
I've chosen to use the widths of both the renderer and editor, and for selected cells with focus, as these tend to give the largest results. Now just use the maximum value per column as the minimum width for the entire column.
That leaves setting the auto resize mode for the table; if your minimum widths don't fit you'll run into problems if you have the auto resize mode set to anything but AUTO_RESIZE_OFF.
Oh, I forgot about the changing of values. Just override setValueAt of the JTable (so you'll have a JTable subclass, probably anonymous):
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1788
|
|
|
Table Column Adjuster
|
 |
sudheer kiran
Ranch Hand
Joined: Jun 26, 2008
Posts: 237
|
|
|
very much thanks you all..your replies are very much useful for me
|
 |
 |
|
|
subject: jtable column size
|
|
|