title: JTable in JScrollpane adjust height of the displayed component
body: <p>I have a jtable within a jscrollpane. I use the jgoodies Form Layout and have put the scrollpane in a row that is set to "pref". This is how the table looks like right now:</p>
  
  <p><img src="https://i.stack.imgur.com/HaMkv.png" alt="enter image description here"></p>
  
  <p>I am using swingx JXTable by the way and have set the visibleRowCount to 2. But only half a row is displayed.</p>
  
  <p>While making the SSCCE I realised that its not working properly because I have a custom TextAreaRenderer. I think because of this renderer, the row height is not being calculated properly. I am trying to figure out why, in the meantime if you guys can spot it that would be great... </p>
  
  <pre><code>public static void main(String[] args) {
      JFrame frame=new JFrame();
      JPanel panel=new FormDebugPanel();
      frame.setContentPane(panel);
      FormLayout layout=new FormLayout("1dlu:grow,200dlu:grow,1dlu:grow",
              "10dlu,pref,5dlu,pref,10dlu");
      panel.setLayout(layout);
      String[] columns= {"1st Column","2nd Column","3rd Column"};
      String[][] data= {
              {"Sethu","Data","Something\nis\nhere"},
              {"Sethu","Data","Something\nis\nhere"},
  
      };
      JXTable table=new JXTable(data,columns);
      table.setVisibleRowCount(2);
  
      TableColumnModel columnModel=table.getColumnModel();
      columnModel.getColumn(2).setCellRenderer(new TextAreaRenderer());
  
      JScrollPane scrlPan=new JScrollPane(table);
      CellConstraints cc=new CellConstraints();
      panel.add(scrlPan, cc.xy(2, 2));
      panel.add(new JLabel("Label After Table"),cc.xy(2,4));
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
    }
  }
  </code></pre>
  
  <p>And here's the renderer:</p>
  
  <pre><code>class TextAreaRenderer extends JTextArea implements TableCellRenderer {
  
  private final DefaultTableCellRenderer adaptee = new DefaultTableCellRenderer();
  
  /** map from table to map of rows to map of column heights */
  private final Map&lt;JTable,Map&lt;Integer,Map&lt;Integer,Integer&gt;&gt;&gt; cellSizes = new HashMap&lt;JTable,Map&lt;Integer,Map&lt;Integer,Integer&gt;&gt;&gt;();
  
  public TextAreaRenderer() {
      setLineWrap(true);
      setWrapStyleWord(true);
  }
  
  public Component getTableCellRendererComponent(//
          JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
      // set the colours, etc. using the standard for that platform
      adaptee.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
      setForeground(adaptee.getForeground());
      setBackground(adaptee.getBackground());
      setBorder(adaptee.getBorder());
      setFont(adaptee.getFont());
      setText(adaptee.getText());
  
      // This line was very important to get it working with JDK1.4
      TableColumnModel columnModel = table.getColumnModel();
      setSize(columnModel.getColumn(column).getWidth(), 100000);
      int height_wanted = (int) getPreferredSize().getHeight();
      addSize(table, row, column, height_wanted);
      height_wanted = findTotalMaximumRowSize(table, row);
      if (height_wanted != table.getRowHeight(row)) {
          table.setRowHeight(row, height_wanted);
      }
      return this;
  }
  
  private void addSize(JTable table, int row, int column, int height) {
      Map&lt;Integer,Map&lt;Integer,Integer&gt;&gt; rows = cellSizes.get(table);
      if (rows == null) {
          cellSizes.put(table, rows = new HashMap&lt;Integer,Map&lt;Integer,Integer&gt;&gt;());
      }
      Map&lt;Integer,Integer&gt; rowheights =  rows.get(new Integer(row));
      if (rowheights == null) {
          rows.put(new Integer(row), rowheights = new HashMap&lt;Integer,Integer&gt;());
      }
      rowheights.put(new Integer(column), new Integer(height));
  }
  
  /**
   * Look through all columns and get the renderer. If it is also a
   * TextAreaRenderer, we look at the maximum height in its hash table for
   * this row.
   */
  private int findTotalMaximumRowSize(JTable table, int row) {
      int maximum_height = 0;
      Enumeration&lt;TableColumn&gt; columns = table.getColumnModel().getColumns();
      while (columns.hasMoreElements()) {
          TableColumn tc = (TableColumn) columns.nextElement();
          TableCellRenderer cellRenderer = tc.getCellRenderer();
          if (cellRenderer instanceof TextAreaRenderer) {
              TextAreaRenderer tar = (TextAreaRenderer) cellRenderer;
              maximum_height = Math.max(maximum_height, tar.findMaximumRowSize(table, row));
          }
      }
      return maximum_height;
  }
  
  private int findMaximumRowSize(JTable table, int row) {
      Map&lt;Integer,Map&lt;Integer,Integer&gt;&gt; rows = cellSizes.get(table);
      if (rows == null)
          return 0;
      Map&lt;Integer,Integer&gt; rowheights = rows.get(new Integer(row));
      if (rowheights == null)
          return 0;
      int maximum_height = 0;
      for (Iterator&lt;Map.Entry&lt;Integer, Integer&gt;&gt; it = rowheights.entrySet().iterator(); it.hasNext();) {
          Map.Entry&lt;Integer,Integer&gt; entry = it.next();
          int cellHeight = ((Integer) entry.getValue()).intValue();
          maximum_height = Math.max(maximum_height, cellHeight);
      }
      return maximum_height;
  }
  }
  </code></pre>
  
  <p>When I use the TextAreaRenderer, then the setVisibleRowHeight() is not honoured correctly. I think it has something to do with not setting the row height properly in the renderer.</p>