title: ActionPerformed on a JButton invoked more than once?
body: <p>I am implementing a function which the function passes a selected item in a JList and a value in a JTextField when users click on a JButton.</p>
  
  <p>I am using several listeners. However, it seems that the loop actionPerformed inside addcartbtn is invoked twice when users pressed the button for the second times and produces unwanted results. When users pressed the third time, the function seems invoked three times.</p>
  
  <pre><code>   list.addListSelectionListener(new ListSelectionListener() {
  
          Map&lt;String, Integer&gt; cartlist = new HashMap&lt;String, Integer&gt;();
  
          public void valueChanged(final ListSelectionEvent e) {
              if (e.getValueIsAdjusting()) {
                  System.out.println("test0");
                  final ArrayList&lt;String&gt; cartArrayList = new ArrayList&lt;String&gt;();
                  addcartbtn.addActionListener(new ActionListener() {
  
                      public void actionPerformed(final ActionEvent e2) {
  
                          System.out.println("test2");
                          String itemselected = "";
                          System.out.println("Index is " + e.getLastIndex());
                          String itemname = (String) hashmap.get(e.getLastIndex());
                          itemselected = itemname;
  
                          //System.out.println(itemselected);
                          try {
                              int insertedquantity = Integer.parseInt(quantity.getText());
                              cartlist.put(itemselected, insertedquantity);
                              //shoppingcart.revalidate();
                              String element = itemselected + " " + String.valueOf(insertedquantity);
  
                              cartArrayList.add(element);
  
                              System.out.println(element);
                              //System.out.println(counter);
                              shoppingcart.setListData(cartArrayList.toArray());
                              shoppingcart.revalidate();
                              shoppingcart.repaint();
                              System.out.println("---------");
  
                          } catch (NumberFormatException ex) {
                              System.out.println("Not a number!");
                          }
                      }
                  });
              }
          }
      });
  </code></pre>
  
  <p>Thanks everyone for helping!</p>