title: How do I dynamically create multiple swing objects and remove them one at a time?
body: <p>I'd like to create a new JPanel with each click of a button and have objects on the panel created as well (buttons and/or labels). That part seems to be technically working, but I run into problems when trying to remove the panels (from last added to first) one at a time.</p>
  
  <p>I've tried to trim down the code as much as possible, though I left in a an unused array because I <em>think</em> that's how I'm suppose to make this work. any advice would be much appreciated. Also let me know if anything is too vague, trying to verbalize my question was harder than i thought.</p>
  
  <p>Disclaimer: at least a few best practices have been ignored, sorry.</p>
  
  <pre><code>public class CreatePanelsTest{
  
      JPanel totalGUI;
      GridLayout grid = new GridLayout(0,1);
      int panelX = 10;
  
       public JPanel createContentPane (){
  
          //create a bottom JPanel to place everything on.
          totalGUI = new JPanel();
          //set the Layout Manager to null, manually place objects
          totalGUI.setLayout(null);    
          JPanel controlPanel = new JPanel();
          controlPanel.setLocation(50, 220);
          controlPanel.setSize(200, 40);
          JButton addSet = new JButton("add set");
               addSet.addActionListener(new java.awt.event.ActionListener() {
              @Override
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  newSetActionPerformed(evt);
              }
          });
           JButton removeSet = new JButton("remove set");
               removeSet.addActionListener(new java.awt.event.ActionListener() {
              @Override
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  removeSetActionPerformed(evt);
              }
          });     
  
          controlPanel.add(addSet);
          controlPanel.add(removeSet);
          totalGUI.add(controlPanel);
          totalGUI.revalidate();
          return totalGUI;
  
      }
  
  private void newSetActionPerformed(java.awt.event.ActionEvent evt) {
          //on button click adds group
          JPanel newPanel = new JPanel(grid);
          newPanel.setLocation(panelX, 10);
          newPanel.setSize(50,200);
          JButton button1 = new JButton("1");
          JButton button2 = new JButton("2");
          newPanel.add(button1);
          newPanel.add(button2);
          totalGUI.add(newPanel);
          totalGUI.validate();
          totalGUI.repaint();
          panelX = panelX+50;
      }
  
  private void removeSetActionPerformed(java.awt.event.ActionEvent evt) {
          //this is suppose to remove one panel at a time
          //totalGUI.remove(newPanel);
          //totalGUI.validate();
          //totalGUI.repaint();
          //panelX = panelX-50;
      }
  
  
   public JPanel[] autoArray(){
          //was hoping to make this method work for me, unused atm
          int n = 6;
          //can i change the value of "n" later or will it break stuff?
          JPanel[] panels = new JPanel[n];
          for (int i = 0; i&lt;n; i++){
              panels[i] = new JPanel(grid);
  
          }
          return panels;
  
      }
  </code></pre>