title: JTable inside a JScrollPane not showing up properly
body: <p>I am working on a the GUI of a piece of code that I have been patching together. I am stuck at this part of the program where I would like a datafile the user chooses to be displayed in a <code>JTable</code> in a preview manner (i.e. the user should not be able to edit the data on the table).</p>
  
  <p>With a button click from Experiment Parameters tab (see screenshot below), I create and run a "PreviewAction" which creates a new tab, and fills it up with the necessary components. Below is the code for <code>DataPreviewAction</code>. <strong>EDIT:</strong> I also posted a <a href="http://pastebin.com/HxTZyMiW" rel="nofollow noreferrer">self-contained, minimal version</a> of this that mimics the conditions in the real project, and exhibits the same behaviour. </p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.util.Arrays;
  import java.util.LinkedList;
  import java.util.List;
  
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.JTabbedPane;
  import javax.swing.JTable;
  import javax.swing.SwingUtilities;
  import javax.swing.table.DefaultTableModel;
  
  public class MyFrame extends JFrame {
  
  private JPanel panel1;
  private JTabbedPane tabs;
  private JButton runButton;
  
  public MyFrame() {
      tabs = new JTabbedPane();
      panel1 = new JPanel();
      runButton = new JButton("go!");
      runButton.addActionListener(new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              runButtonActionPerformed(evt);
          }
      });
      panel1.add(runButton);
      tabs.addTab("first tab", panel1);
      this.add(tabs);
      pack();
  }
  
  public static void main(String args[]) {
      try {
          for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
                  .getInstalledLookAndFeels()) {
              if ("Nimbus".equals(info.getName())) {
                  javax.swing.UIManager.setLookAndFeel(info.getClassName());
                  break;
              }
          }
      } catch (ClassNotFoundException ex) {
          java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
                  java.util.logging.Level.SEVERE, null, ex);
      } catch (InstantiationException ex) {
          java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
                  java.util.logging.Level.SEVERE, null, ex);
      } catch (IllegalAccessException ex) {
          java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
                  java.util.logging.Level.SEVERE, null, ex);
      } catch (javax.swing.UnsupportedLookAndFeelException ex) {
          java.util.logging.Logger.getLogger(MyFrame.class.getName()).log(
                  java.util.logging.Level.SEVERE, null, ex);
      }
  
      /* Create and display the form */
      java.awt.EventQueue.invokeLater(new Runnable() {
  
          public void run() {
              MyFrame frame = new MyFrame();
              frame.setLocationRelativeTo(null);
              frame.setVisible(true);
          }
      });
  }
  
  private void runButtonActionPerformed(java.awt.event.ActionEvent evt) {
      /*
       * Normally there is more stuff happening here but this much will do for
       * the sake of example
       */
      List&lt;String[]&gt; data = new LinkedList&lt;String[]&gt;();
      for (int i = 1; i &lt; 1000; i++)
          data.add(new String[] { "entry1", "value1", "value2", "value3" });
  
      SwingUtilities.invokeLater(new DataPreviewAction(data, tabs));
  }
  
  public class DataPreviewAction implements Runnable {
  
      private JTabbedPane contentHolder;
      private List&lt;String[]&gt; data;
  
      public DataPreviewAction(List&lt;String[]&gt; data, JTabbedPane comp) {
          this.contentHolder = comp;
          this.data = data;
      }
  
      @Override
      public void run() {
          DefaultTableModel previewModel = new DefaultTableModel() {
              @Override
              public boolean isCellEditable(int row, int column) {
                  return false;
              }
          };
  
          for (String[] datarow : data) {
              previewModel.addRow(Arrays.copyOf(datarow, datarow.length,
                      Object[].class));
          }
  
          JTable table = new JTable(previewModel);
  
          JPanel buttonPanel = new JPanel();
          buttonPanel.add(new JButton("A button"));
          buttonPanel.add(new JLabel(
                  "Some description for the awesome table below "));
          buttonPanel.add(new JButton("another button"));
  
          JScrollPane tablePanel = new JScrollPane(table);
          JPanel container = new JPanel();
          container.setLayout(new BorderLayout());
          container.add(buttonPanel, BorderLayout.NORTH);
          container.add(tablePanel, BorderLayout.CENTER);
          contentHolder.addTab("Preview", container);
          contentHolder.validate();
          contentHolder.repaint();
      }
  }
  }
  </code></pre>
  
  <p>There are at least two problems here:</p>
  
  <ol>
  <li>The <code>JTable</code> (or the <code>JScrollPane</code>) does not render at all</li>
  <li>The <code>JScrollPane</code> is not as wide as the frame itself, I have no idea why</li>
  </ol>
  
  <p>I am not all that good in Swing so I might be missing something fundamental. I have checked that the datafile is read properly, and the data model contains the right amount of rows (1000+). SO the table should not be empty. </p>
  
  <p>Suggestions?</p>
  
  <p><img src="https://i.stack.imgur.com/8hkfU.png" alt="screenshot of the problem"></p>