title: Java - How to set JTextArea height to fit its content?
body: <p>I have a JTextArea in a JPanel. The JPanel has a null layout or uses absolute layout.
  The JTextArea has a fixed width. How can I set the JTextArea height to fit its content?</p>
  
  <p>I have this code:</p>
  
  <pre><code>*some random string of varying length*
  String str = "kdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkda"
             + "kdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkda"
             + "kdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkda"
             + "kdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkda"
             + "kdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkda"
             + "kdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkdafkakopwepopkkda";
  
  
  JTextArea wtd = new JTextArea(str);
  wtd.setLineWrap(true);
  wtd.setOpaque(false);
  wtd.setEditable(false);
  wtd.setBorder(new EtchedBorder(EtchedBorder.LOWERED));
  wtd.setLocation(*defined x position*, *defined y position*);
  wtd.setPreferredSize(new Dimension(*fixed width*, *the height depending on the content*));
  wtd.setSize(wtd.getPreferredSize());
  add(wtd);
  </code></pre>
  
  <p>Thanks in advance for any help!</p>