Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Row Height of a JTextArea

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
US
Hi,

I have a JTextArea and set its contents dynamically. I need to dynamically change the height of the JTextArea depending on the number of rows of text in it.

I came up with the idea of getting the desired height value by multiplying the row height the number of rows as follows (ta is my JTextArea):

Code:
ta.setSize(400,ta.getRowHeight() * ta.getRows() );

However, the compiler gives the the following error:
[tt]getRowHeight() has protected access in javax.swing.JTextArea[/tt]

Is there a way to obtain the row height, or do I need to come up with a new solution?

I'm using the null layout manager.

Thanks!

 
You would get access to that method if you were to extend the JTextArea class. Maybe overriding the 'setText' method could be a place to recalculate the controls size using the formula you've got. This may work since you're using a 'null' layout manager. If you were to try to use this technique with a layout manager, it probably wouldn't due to the JTextArea's container requiring a revalidate{} to cater for the controls new size.

Tim
 
Thanks, Tim -- that's a great idea. I'll give it a try.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top