title: How can I remove a JPanel from a JFrame?
body: <p>Recently I asked here <a href="https://stackoverflow.com/questions/2492513/why-i-cannot-add-a-jpanel-to-jframe">how to add a new JPanel to JFrame</a>. The answer helped me to get a working code. But not I have a related question: "How can I remove an old JPanel". I need that because of the following problem.</p>
  
  <p>A new JPanel appears appears when I want (either time limit is exceeded or user press the "Submit" button). But in several seconds some element of the old JPanel appears together with the component of the new JPanel. I do not understand why it happens.</p>
  
  <p>I thought that it is because I have to other threads which update the window. But the first thread just add the old panel once (so, it should be finished). And in the second thread I have a loop which is broken (so, it also should be finished).</p>
  
  <p>Here is my code:</p>
  
  <pre><code>private Thread controller = new Thread() {
      public void run() {
          // First we set the initial pane (for the selection of partner).
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  frame.getContentPane().add(generatePartnerSelectionPanel());
                  frame.invalidate();
                  frame.validate();
              }
          });
          // Update the pane for the selection of the parnter.
          for (int i=40; i&gt;0; i=i-1) {
              final int sec = i;
              SwingUtilities.invokeLater(new Runnable() {
                  public void run() {
                      timeLeftLabel.setText(sec + " seconds left.");
                  }
              });
              try {
                  Thread.sleep(1000);
              } catch (InterruptedException e) { }
  
              if (partnerSubmitted) {
                  break;
              }
          }
          // For the given user the selection phase is finished (either the time is over or form was submitted).
          SwingUtilities.invokeLater(new Runnable() {
              public void run() {
                  frame.getContentPane().add(generateWaitForGamePanel());
                  frame.invalidate();
                  frame.validate();
              }
          });
  
      }
  };
  </code></pre>