title: How can I create a single ActionListener for multiple JButtons
body: <p>I'm creating a basic calculator using MVC. So far I'm adapting a tutorial which merely sums two user entered values together. </p>
  
  <p>Currently each button I'm adding to the view has it's own listener, which is ok. However, the controller as per the tutorial has a single ActionListener inner class per button. This repeats a huge amount of code. </p>
  
  <p>How can I create a single ActionListener class for all buttons pressed, and use a case statement on the id of the button pressed?</p>
  
  <p><strong>Registering the oneButton in the View</strong></p>
  
  <pre><code>void oneListener(ActionListener listenForOneButton){    
      oneButton.addActionListener(listenForOneButton);
  }    
  </code></pre>
  
  <p><strong>Implementing the ActionListener for the oneButton in the Controller inner class</strong></p>
  
  <pre><code>class oneListener implements ActionListener{
      public void actionPerformed(ActionEvent e){
          int previousNumber, displayNumber = 0;          
          try{
              previousNumber = theView.getPreviousDisplayNumber();
              displayNumber = previousNumber+1;               
              theView.setDisplayNumber(displayNumber);
          }           
          catch(NumberFormatException ex){                
              System.out.println(ex);             
              theView.displayErrorMessage("You Need to Enter Integers");              
          }           
      }
  }
  </code></pre>