• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable Auto Column Width

 
Vishaal Kant
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
I want to set the column width of th JTable depending upon the column header lenght. Is is possible to set the column width dynamically depending on the lenght of the header string?
Can somebody send a code snippet please?
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vishaal Kant:
Is is possible to set the column width dynamically depending on the lenght of the header string?


The TableColumn class has a sizeWidthToFit() method that (almost) does what you want, but it has limitations.

limitation 1) It does nothing if the TableColumn has no explicit header renderer. If you are using default renderers, you're out of luck.

limitation 2) Even if the TableColumn has an explicit header renderer, the renderer will receive null instead of a reference to your table, which means it has no way to adjust to any custom fonts that might have been set on the table or the table header.

You can try something like this
but you might be better off setting the preferred column widths yourself.
[ February 22, 2007: Message edited by: Brian Cole ]
 
Brian Cole
Author
Ranch Hand
Posts: 920
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I think it's better to ignore the sizeWidthToFit() method and do something like this instead:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!