I am implementing a function that a function passes the selected item to a JList and value to a JTextField when users click on JButton.
I use several listeners. However, it seems that the actionPerformed loop inside addcartbtn is called twice when users click the button a second time and cause unwanted results. When users clicked a third time, the function is called 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;
Thank you all for your help!
source share