title: java JPanel, dynamically adding components to it and JScrollPane
body: <p>We've been experimenting around with this but without luck, please note I'm probably using the components the wrong way...
  We've got a <code>JFrame</code> cointaining a <code>JPanel</code>, which contains a <code>JScrollPane</code> with another JPanel inside of it, called <code>interiorPanel</code>.
  Having a <code>JButton</code> with an action that adds components dynamically to the innermost panel and then calls its <code>revalidate()</code> method, <code>interiorPanel</code> grows like we need inside of the JScrollPane, showing its scroll bars like this:</p>
  
  <p><img src="https://i.stack.imgur.com/TGgud.png" alt="the desired case"></p>
  
  <p>However, we want to add components with a loop instead of using a button. Using this loop:</p>
  
  <pre><code>for (int i = 1; i &lt; 10; i++) {
      demo.getPanelInterior().add(new JButton("Hello"));
      demo.getPanelInterior().revalidate();
      demo.getPanelInterior().repaint();
  }
  </code></pre>
  
  <p>doesn't seem to work like in the case when we click the button since the innermost panel grows, but the scroll bars don't show up, so we end with something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/ZKMiL.png" alt="undesired effect"></p>
  
  <p>What should we do to get the result from the first image? Here's the working example code:</p>
  
  <pre><code>    import java.awt.*;
      import java.awt.event.*;
  
      import javax.swing.*;
  
  
      public class Demo extends JPanel {
  
          private JPanel exteriorPanel;
          private JPanel interiorPanel;
          private JButton button;
          private JList&lt;String&gt; list;
          private String[] imageNames = { "Bird", "Cat", "Dog", "Rabbit", "Pig", "dukeWaveRed",
          "kathyCosmo", "lainesTongue", "left", "middle", "right", "stickerface"};
          private DefaultListModel&lt;String&gt; model;
  
  
  
          public Demo() {
  
              //Create the list of images and put it in a scroll pane.
  
              model = new DefaultListModel&lt;&gt;();
              for (String s : imageNames) {
                  model.addElement(s);
              }
  
              list = new JList&lt;String&gt;(model);
              list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
              list.setSelectedIndex(0);
  
              JScrollPane listScrollPane = new JScrollPane();
              listScrollPane.setViewportView(list);
  
              button = new JButton("Add element");
              button.addActionListener(new ActionListener() {
  
                  @Override
                  public void actionPerformed(ActionEvent e) {
                      buttonActionPerformed(e);
                  }
              });
  
          interiorPanel = new JPanel();
          interiorPanel.add(listScrollPane);
          interiorPanel.add(button);
          interiorPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 2, 2));
  
          JScrollPane anotherScroll = new JScrollPane();
          anotherScroll.setViewportView(interiorPanel);
  
          exteriorPanel = new JPanel();
          exteriorPanel.add(anotherScroll);
          exteriorPanel.setPreferredSize(new Dimension(400, 200));
  
          interiorPanel.setBackground(new Color(255, 0, 0));
  
  
      }
  
      private void buttonActionPerformed(ActionEvent e) {
          model.addElement("Hola");
          interiorPanel.add(new JButton("Hello"));
          interiorPanel.revalidate();
      }
  
      //Used by SplitPaneDemo2
      public JList getImageList() {
          return list;
      }
  
      /**
       * Create the GUI and show it.  For thread safety,
       * this method should be invoked from the
       * event-dispatching thread.
       */
      private static void createAndShowGUI() {
  
          //Create and set up the window.
          JFrame frame = new JFrame("Demo");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          Demo demo = new Demo();
          frame.getContentPane().add(demo.getExteriorPanel());
  
          //Display the window.
          frame.pack();
          frame.setVisible(true);
  
          for (int i = 1; i &lt; 10; i++) {
              demo.getPanelInterior().add(new JButton("Hello"));
              demo.getPanelInterior().revalidate();
              demo.getPanelInterior().repaint();
          }
  
  
      }
  
      public JPanel getExteriorPanel() {
          return exteriorPanel;
      }
  
      public JPanel getPanelInterior() {
          return interiorPanel;
      }
  
      public static void main(String[] args) {
          //Schedule a job for the event-dispatching thread:
          //creating and showing this application's GUI.
          javax.swing.SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  createAndShowGUI();
              }
          });
      }
  
  
  }
  </code></pre>
  
  <p>Thanks in advance</p>