Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7937169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:27:16+00:00 2026-06-03T22:27:16+00:00

I am implementing a function which the function passes a selected item in a

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:27:18+00:00Added an answer on June 3, 2026 at 10:27 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a function that receives an argument which it needs to convert to
I am implementing a backtrace function in C, which can output caller's info. like
I am implementing a require/load function which retrieves and executes a remote piece of
What is implementing a function? normally, what I know is implementing an interface which
I am implementing a function to recursively reverse a linked-list, but getting seg-fault. typedef
I need some examples of implementing curry function in ruby(1.8.6 or 1.8.7 not 1.9).
I am implementing a logging function and I would like to keep track of
I am currently working with sliding tabs. I am implementing the hover function to
I am implementing a password reset function and would have the key I want
I'm implementing a book search function based on authors. I should return a query

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.