title: Problems with JTable and JScrollpane size
body: <p>I have a <code>JScrollPane</code> with a <code>JTable</code> in it.  In the <code>JTable</code> I have initially 3 rows.  Later on rows get added.</p>
  
  <p>The default <code>JTable</code> with my 3 rows is ugly because <code>JScrollPane</code> calls <code>getPreferredScrollableViewportSize</code> from the client (<code>JTable</code>) and the return value is always the same. That's why my <code>JTable</code>/<code>JScrollpane</code> with 3 rows has this free space, <code>JScrollpane and</code>JTable do not have the same size.</p>
  
  <p>My solution is to make a subclass <code>JTabl</code>e, override <code>getPreferredScrollableViewportSize</code> and return in this function <code>getPreferredSize();</code>.  Now the whole <code>JScrollPane</code> has exactly the size of the 3 row `JTable!</p>
  
  <p>When a row gets added I have the command <code>((JComponent) scrollPane.getParent()).revalidate();</code></p>
  
  <p>The scrollpane grows with the table.</p>
  
  <p>Everything works fine!</p>
  
  <p>But now I want to set a certain layout for the container of the scrollpane (panel):
  <code>myjpanel.setLayout (new BoxLayout(contentPane, BoxLayout.Y_AXIS));</code></p>
  
  <p>When I add this command my whole solution doesn't work anymore.
  The scrollpane hasn't the size of the 3 row table, this free space is there again.</p>
  
  <p>Why is this?</p>
  
  <p>Does anybody have a solution?</p>
  
  <p>CONTINUED</p>
  
  <p>Thank you camickr for pointing me in the right direction. My solution is the following:</p>
  
  <pre><code>    scrollPane = new JScrollPane (table) {          
        public Dimension getMaximumSize () {
          double height = 0;
  
          double width = super.getMaximumSize().getWidth();
          height += columnHeader.getViewSize().getHeight();           
          height += viewport.getView().getPreferredSize().getHeight();
          height += (getViewportBorderBounds().getHeight() * -1);
          Dimension returnValue = new Dimension ((int) width, (int) height);
          return returnValue;
        }
      };
  </code></pre>
  
  <p>It works now at the beginning.
  But when a row is added to the jtable and I call revalidate(); on the jpanel (parent container of the scrollpanel) the jscrollpanel shrinks in height to 1 row + header!
  Has anyone got an idea what is to do in this situation.</p>
  
  <p>CONTINUED</p>
  
  <p>Now I know the problem. It is the viewportBorder. In order to get the whole, exact height I have to total the height of the view (Jtable), the height of the column header and the height of the viewportBorder. The first time <code>getViewportBorderBounds().getHeight()</code> returns -3, next time - after a new row is inserted in the table model - the function returns 48. I do not understand why this function returns 48 now.
  Another point is that camickr says that changing the size of the jscrollpane defeats the purpose of the scrollpane. I do not understand this. How can anyone be satisfied with a default jscrollpane size of 450 x 400 (http://www.javalobby.org/java/forums/t19559.html) even when the jtable has only 3 lines at the beginning. This unfilled extra space does not look very professional in my eyes. Has anyone got a better solution for this. </p>
  
  <p>Thank you very much for your answers</p>
  
  <p>CONTINUED
  Now everything works!
  At the beginning I simply set the jscrollpane border to 0 ... <code>sp.setBorder(createEmptyBorder());</code>
  In this way I avoid having these strange return values of <code>getViewportBorderBounds().getHeight()</code>
  Wolfgang</p>