title: How can I refresh the JPanel?
body: <p>I want to do a dynamic form in Swing. When I call firstly the <code>dataShow</code> method, it creates the GUI. But when I call it again, it keeps the old panel and display the new at the background.</p>
  
  <p>And when I try to remove the current panel, and then add the new. The GUI became empty</p>
  
  <p>A <code>Thread</code> listen to events (int id in that case).</p>
  
  <p>here is my code to display the dynamic form :</p>
  
  <pre><code> public void showData(int id) throws DAOException, ClassNotFoundException{
  
          FormDAOImpl form = new FormDAOImpl();
          String b = form.importTagPoint(id);
          //if(compteur%2 == 0) {System.out.println("Compteur : " +compteur); scrollPane.remove(panel); 
          //frame.getContentPane().remove(scrollPane);
          //}
          panel = new JPanel(new MigLayout());
  
          if(b == null) b = "";
          String[] bits = b.split("\\,");
          String delims = "[=]";
          while(i&lt;bits.length){
              textField = new JTextField();
              String[] bitsS = bits[i].split(delims);
              textField.setText(bitsS[1]);
  
              JLabel label = new JLabel(bitsS[0]+ " :    ");
              panel.add(label);
              panel.add(textField, "span, grow, alignx center, flowx");
              i++;
          }
  
          JButton annuler = new JButton("Annuler");
          JButton enregistrer = new JButton("Enregistrer");
  
          panel.add(annuler);
          panel.add(enregistrer);
          panel.revalidate();
          panel.repaint();
  
          scrollPane = new JScrollPane(panel);
          scrollPane.revalidate();
          scrollPane.repaint();
  
          frame.getContentPane().add(scrollPane);
          //frame.repaint();
          frame.invalidate();
          frame.validate();
          frame.repaint();
  
          frame.pack();
          frame.setMaximumSize(new Dimension(300, 800));
  
          compteur++;
      }
  </code></pre>