Skip to main content

JTextArea, preferredSize, and JScrollPane?

No replies
charliehubbard
Offline
Joined: 2004-05-25

I thought I understood getPreferredSize() and how it relates to JScrollPanes, but I guess I didn't. I know if I set JTable's preferred size that causes problems when your table has more rows than you can display with your static setting, and hence JScrollPane doesn't show any scrollbars. Ok so after thinking about it that makes sense. JTable shouldn't worry about scrolling when laying itself out.

But, after starring at JTextArea's implementation of getPreferredSize() I don't know why you don't have the same problem. JTextArea's getPreferredSize() method calculates it like so: columns * the columnWidth() + rows * rowHeight() + some insets for rows and columns > 0. Otherwise it just returns 400 x 400. Ok so I went and checked JTextArea getPreferredScrollableViewportSize(), but it does almost the exact same thing getPreferredSize() does!

Some how JScrollPane has got to get a Dimension that represents the total size of all the text in the JTextArea. Where is that calculated, what calculates it, and how does JScrollPane get that from JTextArea?

Now, why are these rules different for JTable and JTextArea?

Thanks
Charlie