JTextArea preferred size

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

The Java documentation says that a JTextArea's preferred size is what
is needed to display all the text. Why, then, does a text area
created by

final JTextArea content = new JTextArea("Long string goes here.");
content.setLineWrap(true);
content.setWrapStyleWord(true);

have a preferred size of 100x30, which is absolutely not big enough
to display all the text? What I want is to tell a text area how wide
of a space I want to put it in, and for it to tell me how much
vertical space it needs to display all of its text - how do I do that?
 
K

Knute Johnson

Christopher said:
The Java documentation says that a JTextArea's preferred size is what
is needed to display all the text. Why, then, does a text area
created by

final JTextArea content = new JTextArea("Long string goes here.");
content.setLineWrap(true);
content.setWrapStyleWord(true);

have a preferred size of 100x30, which is absolutely not big enough
to display all the text? What I want is to tell a text area how wide
of a space I want to put it in, and for it to tell me how much
vertical space it needs to display all of its text - how do I do that?

JTextArea is really designed to be used inside of a JScrollPane. That
aside you need to read the documentation to see how the preferred size
is determined.

"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. If the
value for rows or columns is equal to zero, the preferred size along
that axis is used for the viewport preferred size along the same axis."

If you want to use a JTextArea, you need to specify a number of rows and
or columns. This is to allow the size of the component to adjust for
the font being used.

So I think you need to reconsider your GUI design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top