You should put your JTextArea
in a JScrollPane
. This will keep your row size intact, and would have the added benefit of allowing the user to navigate the input. If you want, you can set the scrollpane to never show. You can still rely on the JTextArea
component to calculate the height of the rows and then the preferred height of the component.
The call to setRows
that you use to indicate the amount of rows visible on your display is a property that is maintained to work with JScrollPane
, as described in the JTextArea
JavaDoc:
java.awt.TextArea has two properties rows* and columns that are used to determine the preferred size. JTextArea uses these properties to indicate the preferred size of the viewport when placed inside a JScrollPane to match the functionality provided by java.awt.TextArea. JTextArea has a preferred size of what is needed to display all of the text, so that it functions properly inside of a JScrollPane.
*my emphasis