aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes jtable column size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "jtable column size" Watch "jtable column size" New topic
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: 19718
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2200
    
    7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jtable column size