views:

360

answers:

1

I want my JTextArea to resize itself (expand vertically) when the last line (that the text area's height can offer) is reached and the user wants to start a new line. You know, like the textbox in MSWord.

I have an idea to use getLineCount() and determine (if necessary) the new height of the JTextArea. Do you have, or know of better approaches for implementing this?

+1  A: 

Actually, the JTextArea always has the correct size so all lines of text are visible. What you experience is probably that you wrapped the text area in a JScrollPane. Just omit the scroll pane and make the text area a direct child of the container.

Another solution is to listen to resize events of the text area and size the scroll pane accordingly. This way, you can grow to a certain size and then start to display scroll bars (for example, when someone pastes 500KB of text into the text area).

Aaron Digulla
I just add the JTextArea to a JPanel. Is it something with setting the size? But if I don't set the size, then it does not show up.
Halo
You just have to set the size of the JPanel so that the text area can grow. The text area should automatically adjust to fit the size of the text inside.
Aaron Digulla
well, it didn't work. BTW I switched to using JTextPane, but still the problem with the expansion continues
Halo
hmm, I think it's a problem with setting the text. I use setText() but probably i should use insertString(). i'll try that
Halo
nope, it didn't work either
Halo
Please post the code in your question
Aaron Digulla