title: I want to retrieve values from my JButton extension
body: <p>I have read that <code>ActionEvents</code> return the reference of the event object. I want to know how to extract a piece of information out of that reference. I'm sure the answer is out there, I just wasn't sure how to word my question to get it.</p>
  
  <p>I have created a new class that extends <code>JButton</code>. I want each button I create to store an integer value for math operations.</p>
  
  <pre><code>import javax.swing.JButton;
  public class NewButton extends JButton {
      int value;
      public NewButton(String writing, int value) {
          this.setText(writing);
          this.value = value;
      }
  
      public int getValue() {
          return value;
      }
  }
  </code></pre>
  
  <p>I want to use an <code>ActionListener</code> to check for a click then I want to display the int <code>value</code> in the console. Here is my GUI:</p>
  
  <pre><code>import javax.swing.JFrame;
  import javax.swing.JButton;
  import javax.swing.JPanel;
  import java.awt.event.ActionListener;
  import java.awt.event.ActionEvent;
  
  public class MyGui {
      public static void main(String[] args) {
  
          JFrame frame = new JFrame("GUI");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          JPanel panel = new JPanel();
          frame.add(panel);
  
          NewButton button = new NewButton("ten", 10);
          panel.add(button);
  
          ActionListener listener = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  if(e.getSource() == button) {
                      System.out.println(button.getValue());
                  }
              }
          };
  
          button.addActionListener(listener);
  
          frame.setVisible(true);
          frame.setSize(100,100);
      }
  }
  </code></pre>
  
  <p>So this works fine for one button. </p>
  
  <p>Now we get to my problem (finally). I want to make another <code>NewButton</code>,<code>button2</code> . I thought I would change my <code>ActionListener</code> section. I figured I could include one statement for both buttons, since they are of the same class.</p>
  
  <pre><code>        ActionListener listener = new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  if(e.getSource() == button1 || e.getSource() == button2) {
                      System.out.println(e.getSource().getValue());
                  }
              }
          };
  </code></pre>
  
  <p>But this does not work.</p>
  
  <p>My reasoning was that since I can check:</p>
  
  <pre><code>e.getSource() == button
  </code></pre>
  
  <p>then the reference <code>e.getSource()</code> should give me access to my <code>getValue()</code>.</p>
  
  <p>Does anyone know a way to get the <code>value</code> from <code>e.getSource()</code>? My goal was to see if I could avoid making separate if statements or action listeners for each button.</p>