title: What is the relation between ContentPane and JPanel?
body: <p>I found one example in which buttons are added to panels (instances of <code>JPanel</code>) then panels are added to the the containers (instances generated by <code>getContentPane()</code>) and then containers are, by the construction, included into the <code>JFrame</code> (the windows).</p>
  
  <p>I tried two things: </p>
  
  <ol>
  <li><p>I got rid of the containers. In more details, I added buttons to a panel (instance of <code>JPanel</code>) and then I added the panel to the windows (instance of <code>JFrame</code>). It worked fine.</p></li>
  <li><p>I got rid of the panels. In more details, I added buttons directly to the container and then I added the container to the window (instance of <code>JFrame</code>).</p></li>
  </ol>
  
  <p>So, I do not understand two things.</p>
  
  <ol>
  <li><p>Why do we have two competing mechanism to do the same things?</p></li>
  <li><p>What is the reason to use containers in combination with the panels (<code>JPanel</code>)? (For example, what for we include buttons in JPanels and then we include JPanels in the Containers). Can we include <code>JPanel</code> in <code>JPanel</code>? Can we include a container in container?</p></li>
  </ol>
  
  <p><strong>ADDED:</strong></p>
  
  <p>Maybe essence of my question can be put into one line of code:</p>
  
  <pre><code>frame.getContentPane().add(panel);
  </code></pre>
  
  <p>What for we put <code>getContentPane()</code> in between? I tried just <code>frame.add(panel);</code> and it works fine.</p>
  
  <p><strong>ADDED 2:</strong></p>
  
  <p>I would like to add some code to be more clear about what I mean. In this example I use only JPane:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  public class HelloWorldSwing {
      public static void main(String[] args) {
          JFrame frame = new JFrame("HelloWorldSwing");
          JPanel panel = new JPanel();
          panel.setLayout(new BorderLayout());        
          panel.add(new JButton("W"), BorderLayout.NORTH);
          panel.add(new JButton("E"), BorderLayout.SOUTH);
          frame.add(panel);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>And in this example I use only Content Pane:</p>
  
  <pre><code>import java.awt.*;
  import javax.swing.*;
  public class HelloWorldSwing {
      public static void main(String[] args) {
      JFrame frame = new JFrame("HelloWorldSwing");
      Container pane = frame.getContentPane();
      pane.setLayout(new BorderLayout()); 
      pane.add(new JButton("W"), BorderLayout.NORTH);
      pane.add(new JButton("E"), BorderLayout.SOUTH);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.pack();
      frame.setVisible(true);
      }
  }
  </code></pre>
  
  <p>Both work fine! I just want to know if between these two ways to do things one is better (safer).</p>