title: JScrollPanes, JTextAreas, and maximum sizes
body: <p><em>Context</em>: In a flashcard application, I have a view for a <code>CardData</code> (a data structure representing one side of a flashcard). It has, in its most basic form, a <code>String text</code>. The view (known as <code>CardDataView</code>) consists of the <code>text</code> in an uneditable <code>JTextArea</code> inside a <code>JScrollPane</code>.</p>
  
  <p>At one point, I need to line up (vertically) a bunch of these <code>CardDataView</code>s, so I put them in a vertical <code>Box</code>.</p>
  
  <p>Here's the problem: when I expand the window from it's "preferred" size (as determined by <code>pack</code>) and then size it back down, the JScrollPane adds a horizontal scroll bar and allows me to scroll horizontally in  the text field. Essentially, <strong>it's sizing up the text area when it shouldn't be.</strong></p>
  
  <p>Here's my code (stripped down and simplified):</p>
  
  <pre><code>public class DebugTest {
  
      public static void main(String[] args) {
          CardDataView cdv = new CardDataView(new CardData(
                  "Lorem ipsum dolor sit amet filler filler filler"));
          JFrame frame = new JFrame();
          frame.add(new JScrollPane(cdv), BorderLayout.CENTER);
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }
  
  }
  
  class CardData {
      private String text; // text on the card
  
      /*
       * NOTE: This class has been simplified for SSCCE purposes. It's not really
       * just a String field in production; rather, it has an image and other
       * properties.
       */
  
      public CardData(String text) {
          super();
          this.text = text;
      }
  
      public String getText() {
          return text;
      }
  
  }
  
  class CardDataView extends Box {
      private JTextArea txtrText; // the text area for the text
  
      /*
       * NOTE: As with CardData, this class has been simplified. It's not just a
       * JTextArea; it's also an ImageView (custom class; works fine), among other
       * things.
       */
  
      public CardDataView(CardData data) {
          super(BoxLayout.Y_AXIS);
          txtrText = new JTextArea();
          txtrText.setLineWrap(true);
          txtrText.setRows(3);
          txtrText.setEditable(false);
          txtrText.setWrapStyleWord(true);
          final JScrollPane scrollPane = new JScrollPane(txtrText);
          add(scrollPane);
          txtrText.setText(data.getText());
      }
  }
  </code></pre>
  
  <p>Now, something that <em>really</em> confuses me (unless I'm going about it wrong) is that if I essentially inline the data and view classes, by just creating a text area with text and a scroll pane (even with setting the same number of rows, same wrap settings, etc.), it behaves as expected. What could be happening?</p>
  
  <p>Concerns:
  - All imports are in order.
  - If you're wondering why <code>CardLayoutView</code> is a <code>Box</code>: in production, it has more than just a text box. The same error happens when it's a <code>JPanel</code> and I <code>setLayout</code> instead of <code>super</code>.
  - The same glitch happens if I don't use the <code>CardData</code> class at all (and set the text area's text manually).</p>
  
  <p>Any ideas?</p>