title: Deleting selected checkboxes using java swing
body: <p>I am new to java swing. I have a code that generates checkboxes. I want to have a button somewhere in my frame, which on clicking, should delete the selected checkbox entries. Here is what I have so far.</p>
  
  <pre><code>public class Scroll extends JPanel  {  
  
  
  public static void main(String[] args) {   
       SwingUtilities.invokeLater(new Runnable(){  
           public void run()    {  
               createAndShowGUI();   
               }   
           });   
       }   
   public static void createAndShowGUI()     {  
       JFrame frame = new JFrame("JFrame with ScrollBar"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
       JComponent newContentPane = new ResultButtonBar();
       newContentPane.setOpaque(true);
       JScrollPane scrollPane = new JScrollPane(newContentPane); 
       scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);  
       frame.getContentPane().add(scrollPane);  
       frame.setSize(800, 800); 
       frame.setVisible(true);
       JButton startButton = new JButton("Start");
       frame.add(startButton, BorderLayout.SOUTH);
       startButton.addActionListener(new ActionListener() {
  
          @Override
          public void actionPerformed(ActionEvent arg0) {
              // TODO Auto-generated method stub
  
              JOptionPane.showMessageDialog(null, "basdsadad");
  
                  }
          });
  
      }
  
    } 
  </code></pre>
  
  <p>and the new ResultButtonBar().java </p>
  
  <pre><code>public class ResultButtonBar extends JPanel  {   
  
    private HashMap&lt;JCheckBox, ArrayList&lt;Integer&gt;&gt; map = new HashMap&lt;&gt;();
      private JLabel _label;
  
      private static final int MAX_CHECKS = 1000;
  
      public ResultButtonBar() {
          super();
  
          JButton btn = new JButton();
          btn.setVisible(true);
  
          JCheckBox checkBox;
          Random r = new Random();
  
          JPanel checkPanel = new JPanel(new GridLayout(0, 1));
          _label = new JLabel("You selected nothing");
          checkPanel.add(_label);
  
          for (int i = 0; i &lt; MAX_CHECKS; i++) {
              StringBuilder sb = new StringBuilder();
              ArrayList&lt;Integer&gt; a = new ArrayList&lt;&gt;();
              for (int j = 0; j &lt; 2; j++) {
                  Integer temp = (r.nextInt()) % 100;
                  a.add(temp);
                  sb.append(temp).append(" ");
              }
  
              checkBox = new JCheckBox(sb.toString().trim());
              checkBox.setName("CheckBox" + i);
  
              map.put(checkBox, a);
              checkPanel.add(checkBox);
          }
  
          add(checkPanel);
  
      }
  
  }
  </code></pre>