Essentially, txtArea.setPreferredSize(new Dimension(900, 500));
is removing the automatic calculations employed by JTextArea
that it uses to determine the amount of space it needs to display all the text. You are effectivly saying, there is only 500 pixels worth of height that will ever be needed.
You "could" set the preferred size of the scroll pane, but that's not really recommended. Instead, you want to change the value returned by getPreferredScrollableViewportSize
in the JTextArea
This tells the scroll pane how big to make the viewable area ... if it can...
JTextArea txtArea = new JTextArea() {
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(900, 500);
}
};
Take a look at Scrollable for more details
Updated
As AndrewThompson has pointed out, a better (and preferred way) would be to simply specify the rows and columns for the JTextArea
and let it figure out what that means based on the platforms rendering capabilities...
JTextArea txtArea = new JTextArea(40, 100);
Yea for simplicity...