title: Java remove listener from button after 3 clicks
body: <p>So I want to remove a listener from a button after the button has been pressed 3 times.
  So far I have this</p>
  
  <pre><code>class Q5 
  {
  JFrame frame;
  JButton button;
  int clickCount = 0;
  
  public static void main (String[] args)
  {
      Q5 example = new Q5();
      example.go();
  }
  
  public void go()
  {
      frame = new JFrame();
  
      button = new JButton ("Should I do it");
      button.addActionListener(new ButtonPressListener());
      button.addActionListener(new AngelListener());
      button.addActionListener(new DevilListener());
      button.addActionListener(new ConfusedListener());
  
  
  
      frame.getContentPane().add(BorderLayout.CENTER, button);
      frame.setVisible(true);
      frame.setSize(400,150);
      // set frame properties here
  }
  
  class ButtonPressListener implements ActionListener 
  {
      public void actionPerformed(ActionEvent event) 
      {
          clickCount++;
      }
  }
  
  class AngelListener implements ActionListener 
  {
      public void actionPerformed(ActionEvent event) 
      {
          System.out.println("Don't do it, you might regret it!");
      }
  }
  
  class DevilListener implements ActionListener 
  {
      public void actionPerformed(ActionEvent event) 
      {
          System.out.println("Go on, do it!");
      }
  }
  
  class ConfusedListener implements ActionListener   
  {
      public void actionPerformed(ActionEvent event) 
      {
          if(clickCount &gt; 3)
          {
              for(ConfusedListener conf : button.getActionListeners())
              {
                  button.removeActionListener(conf);
              }
          }
          else
              System.out.println("I don't know!");
      }
  }
  </code></pre>
  
  <p>The way I read online was do a for loop, as I tried above, however I get a type mismatch. Most of the examples I could find were about removing all of the listeners, however I only want to remove the ConfusedListener from the button. Other than the for loop above, I don't have any ideas of how to do it.</p>