title: Fetching actionPerformed from another class
body: <p>I have a class that implements actionPerfomed and gives a certain functionality to buttons. In the same class I have a constructor that when called I want it to "fetch" the actionPerformed that lives in another class and gives a different functionality to my buttons. </p>
  
  <p>The code in my constructor looks like this:</p>
  
  <pre><code>    button1.addActionListener(new MyButtonActionListener());
  </code></pre>
  
  <p>where <code>new MyButtonActionListener()</code> contains the the actionPerformed that I want to call. Problem is that nothing happens when I click on the button. 
  I am just playing around and don't know if this can even be done. Thanks!</p>
  
  <pre><code>public class ButtonFrameActionListener extends JFrame implements ActionListener {
  
      private MyButton button1 = new MyButton();
      private MyButton button2 = new MyButton();
      private MyButton[] buttons;
  
      public ButtonFrameActionListener(){
          super("A button window");
          setPreferredSize(new Dimension(400,300));
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          JPanel myPanel = new JPanel();
  
          myPanel.add(button1);
          myPanel.add(button2);
  
          button1.addActionListener(new MyButtonActionListener());
          button2.addActionListener(new MyButtonActionListener());
  
          buttons = new MyButton[2];
          buttons[0] = button1;
          buttons[1] = button2;
  
          this.add(myPanel);
          this.pack();
          this.setVisible(true);
      }
  
  public void actionPerformed(ActionEvent e){
      for(int i = 0; i&lt;buttons.length; i++){
          if(e.getSource() != buttons[i]){
              buttons[i].toggleState();
          }
      }
  }
  </code></pre>
  
  <p>//****************************************************************************//</p>
  
  <pre><code>public class MyButtonActionListener extends MyButton implements ActionListener {
  
      public MyButtonActionListener(Color col1, Color col2, String text1, String text2){
          super(col1, col2, text1, text2);
          /**
           * listens for an action to be performed from actionPerformed.
           * does not come into play before the button is clicked.
           */
          this.addActionListener(this);
      }
  
      public MyButtonActionListener(){
          this(Color.red, Color.yellow, "Press me", "Do it again!");
      }
  
      public void actionPerformed(ActionEvent e){
          this.toggleState();
      }
  }
  </code></pre>
  
  <p>//********************************************************//</p>
  
  <pre><code>public class MyButton extends JButton {
  private Color col1;
  private Color col2;
  private String text1;
  private String text2;
  Boolean pressed = false;
  
  public MyButton(Color col1, Color col2, String text1, String text2){
      this.col1 = col1;
      this.col2 = col2;
      this.text1 = text1;
      this.text2 = text2;
      this.setText(text1); // states the text for the initial state
      this.setBackground(col1); // states the color for the initial state
  }
  
  public MyButton(){
      this(Color.red, Color.yellow, "Press me", "Do it again!");
  }
  
  /**
   * decide the state of the button
   */
  public void toggleState(){
      pressed = !pressed; // pressed equals true
      if(!pressed){ // if pressed equals false
          this.setText(text1);
          this.setBackground(col1);
      } else {
          this.setText(text2);
          this.setBackground(col2);
      }
  }
  </code></pre>
  
  <p>}</p>