Auto Resize JTextArea

I want my JTextArea to resize itself (expand vertically) when the last line is reached (the height of the text area), and the user wants to start a new line. You know how to text box in MSWord.

I have an idea to use getLineCount()and define (if necessary) the new height of JTextArea. Do you have or know the best approaches to implement this?

+3
source share
3 answers

Actually, it JTextAreaalways has the correct size, so all lines of text are visible. You probably experience that you wrapped the text area in JScrollPane. Just lower the scroll area and make the text area a direct child of the container.

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

+8
source

. , JTextArea . , -, (JPanel ). JTextArea . :

java.awt.TextArea : , . JTextArea , , JScrollPane, , java.awt.TextArea. JTextArea , , JScrollPane. , .

+1

, JTextArea JScrollPane JTextArea, , .

, , JTextArea JScrollPane JTextArea, JScrollPane.

0
source

Source: https://habr.com/ru/post/1740334/


All Articles