Number of lines in a JTextArea when word wrap is on
I'm using a JTextArea to render cells in a JList. The cells hold a title and a blurb of text, which can be anywhere from zero to about 4 lines long. The blurbs are in the JTextArea with word wrap set on.
Where I'm having problems is that I want the height of the JTextArea to adjust to the length of the wrapped text, which isn't happening. What I see is that each blurb is cut off at one line. I can setRows(3), for example and make every blurb 3 lines, but that's ugly and wastes screen-space.
If I could ask the JTextArea or its underlying document how many lines it's supposed to be **after wrapping** then I could call setRows(n) to the right n in the getListCellRendererComponent() method of my ListCellRenderer.
I always get 1 from getLineCount(), which I take to mean there are no line breaks in the text.
It would be even more fun if I could call a method like this:
Thanks for any hints that save me from pulling my hair out!