title: Java Swing add/remove jButtons on runtime
body: <p>My application has a module which allows the user to add jButtons on the jLayeredpane during runtime. I want to add action listeners to this dynamically added contents and also i have to provide access to delete the dynamically added buttons during runtime. Is there any way to do this ?</p>
  
  <pre><code>private Map&lt;String, JButton&gt; dynamicButtons;
  
  public void addButton(String name) {
      JButton b = new JButton(name);
      b.addActionListener(new java.awt.event.ActionListener() {
  
          public void actionPerformed(java.awt.event.ActionEvent evt) {
              jButton1ActionPerformed(evt);
          }
      });
  
      jLayeredPane2.add(b);
      dynamicButtons.put(name, b);
      jLayeredPane2.invalidate();
  }
  
  public void removeButton(String name) {
      JButton b = dynamicButtons.remove(name);
      jLayeredPane2.remove(b);
      jLayeredPane2.invalidate();
  }
  </code></pre>