• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jtable column size

 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Rob Spoor
Sheriff
Pie
Posts: 20661
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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):
 
Rob Camick
Ranch Hand
Posts: 2615
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Table Column Adjuster
 
sudheer kiran
Ranch Hand
Posts: 237
Firefox Browser MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very much thanks you all..your replies are very much useful for me
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic