• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jtable column size

 
Ranch Hand
Posts: 237
MyEclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 22787
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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):
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Table Column Adjuster
 
sudheer kiran
Ranch Hand
Posts: 237
MyEclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
very much thanks you all..your replies are very much useful for me
reply
    Bookmark Topic Watch Topic
  • New Topic