user1311794
user1311794

Reputation: 199

ActionPerformed on a JButton invoked more than once?

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.

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.

   list.addListSelectionListener(new ListSelectionListener() {

        Map<String, Integer> cartlist = new HashMap<String, Integer>();

        public void valueChanged(final ListSelectionEvent e) {
            if (e.getValueIsAdjusting()) {
                System.out.println("test0");
                final ArrayList<String> cartArrayList = new ArrayList<String>();
                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!");
                        }
                    }
                });
            }
        }
    });

Thanks everyone for helping!

Upvotes: 4

Views: 2404

Answers (2)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Don't add an ActionListener inside of a ListSelectionListener -- makes no sense. You'll be adding many many listeners for no purpose. In fact, if you want the action to occur when the button is pressed only, I see no reason for a ListSelectionListener at all. Just use an ActionListener that has been added once to the JButton, perhaps in a constructor or set up method.

Also, a little less indentation may make your code easier for us to read.
Edit: I've reduced the code indentation in your original post.

Upvotes: 3

JB Nizet
JB Nizet

Reputation: 691765

You're adding a new action listener to your addcartbtn (wouldn't it be much more readable if it was named addCartButton, BTW) each time a selection is made in the JList. The listener should only be added once.

Upvotes: 3

Related Questions