title: How can I create a JTextArea with a specified width and the smallest possible height required to display all the text?
body: <p>In all the examples that I can find that use a <code>JTextArea</code>, the height &amp; width is known before constructing the <code>JTextArea</code>, and if the <code>JTextArea</code> would require more height, then it is put inside of a <code>JScrollPane</code>. Obviously, the height of <code>JTextArea</code> is dependent on the width and the text contents. </p>
  
  <p>Now, my situation requires that I do not use a <code>JScrollPane</code>, but instead that the <code>JTextArea</code> be just tall enough to display all the text. When I create the <code>JTextArea</code>, I know the text contents and how much width it will have to work with; I don't know the height - I want that to be as small as possible without cutting off any of the text. This seems very difficult to accomplish.</p>
  
  <p>As a side note, the <code>JTextArea</code> will be added to a <code>JPanel</code> that does not have a layout manager - it uses absolute positioning based on the added component's preferred size. This requires that my <code>JTextArea</code> would return the correct dimensions on <code>getPreferredSize()</code>.  The correct dimensions should be the width that I provided when I constructed it, and the minimum height that is required to display all the text with the provided width.</p>
  
  <p>I've found some similar threads that discuss the oddities/bugs involved with the <code>JTextArea</code> that are sometimes solved by calling <code>pack()</code> twice on the parent container. This is not an option for me. I'm tempted to basically create my own <code>JTextArea</code> that takes a width and String and computes the necessary minimum height based on the width and font settings, but I figured I would ask around first before spending the time to do that.</p>
  
  <p>Hopefully my question is clear. Thank you all for your help!</p>