This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Setting size to fit text (JTextArea) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Setting size to fit text (JTextArea)" Watch "Setting size to fit text (JTextArea)" New topic

Setting size to fit text (JTextArea)

Jean Hansen

Joined: May 29, 2002
Posts: 2
I need to set the height of a JTextArea with fixed width to fit a text inside.
How can I do it ?
Actually what I�m trying do to is to make cells multiline in a JTable without specifing a fixed row height. It should be detected depending on columns widths and text length of each cell.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

"jeanhansen" -

Welcome to the JavaRanch! Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it here.

You should be able to call setPreferredSize() in the getTableCellRendererComponent() method of your renderer... since you have a reference to the table, row, column and the value that is going to be displayed you should be able to calculate it. I think that if you call super.getPreferredSize() it will return the size needed for the text... not sure if it takes into account the set width, though...

Thanks! and welcome to the JavaRanch!


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
It is sorta covered in the JavaRanch Style Guide.
subject: Setting size to fit text (JTextArea)
It's not a secret anymore!