This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable - sizing column to fit data  RSS feed

 
Dave Rivera
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have extended the DefaultTableCellRenderer and am attempting to resize the width of the JTable column to fit the width of widest string in the column.
Within the SetValue() method, I coded the following:
<CODE>
if (this.getWidth() < currWidth)
{
this.setPreferredSize(new Dimension(currWidth,this.getHeight()));
}
</CODE>
I believe that all I have succeeded in doing is adjusting the preferred size of the JLabel which the default renderer extends.
Is it possible to effect changes in the column of the JTable?
Thanks for any light someone can shed.
Dave
 
Ashwin Desai
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
You can subclass the DefaultTableColumnModel and provide that kind of functionality. An example follows :

Then create an instance of this class and add this object as a columnmodel for the table.

Hope this helps.
Ashwin.
[This message has been edited by Ashwin Desai (edited May 02, 2001).]
[This message has been edited by Ashwin Desai (edited May 02, 2001).]
[This message has been edited by Ashwin Desai (edited May 02, 2001).]
[This message has been edited by Ashwin Desai (edited May 02, 2001).]
 
Dave Rivera
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Ashwin.
Unfortunately, I am trying to resize columns based on the width, in pixels, of the inserted data string values as opposed to the column headers.
I am leaning towards putting the code in the getTableCellRendererComponent method, where I will have access to the cell value and FontMetrics.
Thanks again.
Dave
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!