title: JTable inside a JScrollPane with a fixed width
body: <p>I have a JTable with a few columns and potentially very many rows. I only want to display 3 rows at a time, and I want the width of the JTable to be 400, <strong>including the width of the scroll bar</strong>.</p>
  
  <p>Here is the code I have so far:</p>
  
  <pre><code>public class SwingDrivesMeMad
  {
      public static void main(String[] args)
      {
          Object[][] rowData = { {"Homer", "beer"},
                                 {"Marge", "wine"},
                                 {"Bart", "coke"},
                                 {"Lisa", "water"},
                                 {"Maggie", "milk"} };
          Object[] headerData = { "name", "beverage" };
  
          JTable table = new JTable(rowData, headerData)
          {
              public Dimension getPreferredScrollableViewportSize()
              {
                  return new Dimension(400, getRowHeight() * 3);
              }
          };
  
          JScrollPane scrollPane = new JScrollPane(table);
          JFrame frame = new JFrame("learn you a barkeep for great good!");
          frame.add(scrollPane);
          frame.pack();
          frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>This is how it looks:</p>
  
  <p><img src="https://i.imgur.com/dI34ABd.png" alt=""></p>
  
  <p>The height is perfect. Unfortunately, the width is 400 <strong>plus</strong> the width of the scrollbar, with a total of 415. This messes up the layout, because there is a graphics area with a fixed width of 400 directly above the table, and both components should line up perfectly. Having the table be slightly bigger just looks bad.</p>
  
  <p>Of course I could simply set the width to 385, because on my system, scrollbars appear to take 15 pixels, but that does not seem very portable. Is there a better solution?</p>