title: Adding an ActionListener to a custom component
body: <p>I have a custom component (which <code>extends JPanel</code>).</p>
  
  <p>It looks something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/hDIa5.png" alt="Custom Component"></p>
  
  <p>My question is: When I use it in another <code>JFrame</code>, how do i add an <code>ActionListener</code> to it, so that when I click any of the button it'll execute code in the <code>actionPerformed</code>?</p>
  
  <p>I tried adding a <code>MouseListener</code> to it, but it didn't work.</p>
  
  <p>EDIT: I'm sorry I'm not very clear on the component:
  It consist of two <code>JButtons</code> (the "-" and "+") and a <code>JTextField</code>.
  When the user clicks on either of them, the number in the middle increases or decreases.
  All these 3 components are placed inside a <code>JPanel</code>.</p>
  
  <p>EDIT 2: Here is the source code: </p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.Font;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  
  import javax.swing.JButton;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.SwingConstants;
  import javax.swing.border.EtchedBorder;
  
  @SuppressWarnings("serial")
  public class ZoomSelector extends JPanel {
      protected JButton btnDecrement;
      protected JTextField txtNumber;
      protected JButton btnIncrement;
  
      private String currentValue, previousValue;
  
      public ZoomSelector() {
          setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
          initGUI();
  
          txtNumber.setText("12");
          currentValue = "12";
  
          setSize(new Dimension(125, 26));
          validate();
      }
  
      public boolean hasChanged() {
          if(currentValue.equals(previousValue)) {
              return true;
          }
          else {
              return false;
          }
      }
  
      public String getText() {
          return txtNumber.getText();
      }
  
      public void reset() {
          previousValue = txtNumber.getText();
          txtNumber.setText("12");
          currentValue = "12";
      }
  
      @Override
      public void setEnabled(boolean e) {
          btnDecrement.setEnabled(e);
          txtNumber.setEnabled(e);
          btnIncrement.setEnabled(e);
      }
  
      public void increment() {
          previousValue = txtNumber.getText();
          int number = Integer.parseInt(txtNumber.getText());
          if(number &lt; 20) {
              number++;
              txtNumber.setText("" + number);
              currentValue = "" + number;
          }       
      }
  
      public void decrement() {
          previousValue = txtNumber.getText();
          int number = Integer.parseInt(txtNumber.getText());
          if(number &gt; 0) {
              number--;
              txtNumber.setText("" + number);
              currentValue = "" + number;     
          }       
      }
  
      private void initGUI() {
  
          btnDecrement = new JButton("-");
          btnDecrement.setFont(new Font("Tahoma", Font.BOLD, 14));
          btnDecrement.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent e) {
                  decrement();
              }
          });
          setLayout(new BorderLayout(0, 0));
          add(btnDecrement, BorderLayout.WEST);
  
          txtNumber = new JTextField();
          txtNumber.setHorizontalAlignment(SwingConstants.CENTER);
          txtNumber.setText("0");
          txtNumber.setEditable(false);
          add(txtNumber, BorderLayout.CENTER);
          txtNumber.setColumns(2);
  
          btnIncrement = new JButton("+");
          btnIncrement.setFont(new Font("Tahoma", Font.BOLD, 14));
          btnIncrement.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  increment();
              }
          });
          add(btnIncrement, BorderLayout.EAST);
      }
  }
  </code></pre>