title: How do I dynamically resize the JFrame to include JPanel with varying content size?
body: <p>How do I make my <code>JFrame</code> match the size of the <code>JPanel</code> which holds the dynamically sized content? </p>
  
  <p>I have created two simple class snippets if anyone could guide me through them. </p>
  
  <h2>Class1 : CanvasExample.java</h2>
  
  <pre><code>import java.awt.Dimension;
  import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.SwingUtilities;
  public class CanvasExample {
      public static void main(String[] args) {
          JPanelEx dataOut = new JPanelEx();
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  JFrame f = new JFrame("Title!");
                  f.setMinimumSize(new Dimension(200,100));
                  f.getContentPane().add(dataOut);
                  f.pack();
                  f.setVisible(true);
              }
          });
      }
  }
  </code></pre>
  
  <h2>Class 2:  JPanelEx.java</h2>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JPanel;
  import javax.swing.SwingUtilities;
  
  public class JPanelEx extends JPanel {
     public JPanelEx() {
           super(new FlowLayout());
     }
     public void paintComponent(Graphics g) {
          super.paintComponent(g); 
          g.drawString("Hello1", 10, 10);
          g.drawString("Hello2", 10, 30);
          g.drawString("Hello3", 10, 50);
          g.drawString("Hello4", 10, 70);
          g.drawString("Hello5", 10, 90);
          g.drawString("Hello6", 10, 110);
          g.drawString("Hello7", 10, 130);
          g.drawString("Hello8", 10, 150);
          g.drawString("Hello9", 10, 170);
          g.drawString("Hello10", 10, 190);
          g.dispose();
    }
  }
  </code></pre>
  
  <h3>Current Output :</h3>
  
  <p><img src="https://i.stack.imgur.com/O8xoW.png" alt="Current Output"></p>
  
  <h3>Expected Output :</h3>
  
  <p><img src="https://i.stack.imgur.com/7tKic.png" alt="Expected Output"></p>
  
  <p>I understand similar question might have been asked many times but I scanned through and couldn't get a clear solution. Any help is appreciated. </p>