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 6165229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:07:47+00:00 2026-05-23T22:07:47+00:00

I have this code which applies a counter to each item on the list.

  • 0

I have this code which applies a counter to each item on the list. When the item reaches a certain number it is moved from jList3 to jList 1.

public Map<Object, Integer> buttonMap = new HashMap<Object, Integer>();
    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {  

    Integer counter = null;
    int[] selection = jList3.getSelectedIndices();
     for (int i = 0; i < selection.length; i++){
        Object selString = jList3.getModel().getElementAt(selection[i]);
       counter = buttonMap.get(selString);      
        if(counter == null ) {           
            buttonMap.put(selString, new Integer(1));     
        }                                                                                                      
        else {
        buttonMap.put(selString, new Integer(counter.intValue() + 1)); 
        }           
        System.out.println(selString + " has been clicked " + buttonMap.get(selString) + " times.");                                      

       try{
        if (counter == 4){                          
           listModel2.removeElement(selString);
            listModel.addElement(selString);                             
        }
       }
        catch (NullPointerException npe1) {
        npe1.getMessage();
              }                                                                  
        }                                                   
  } 

The behavior comes in the if counter == 4 section.

It works fine but here is the weird part that I need help understanding

If I am counting up two items at the same time and they both reach the number that moves them on the same button click.

-It moves 1 of the items

-It does not count up on the other

-Instead it adds +1 to the counter of a non highlighted item

Example:

I am counting on list item 1 and 2, they both reach the max number, 1 gets moved 2 stays put(no increase in count) and item 3 gets +1 to counter

  • 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-05-23T22:07:49+00:00Added an answer on May 23, 2026 at 10:07 pm

    When you remove an element from jList3, the elements that follow are shifted.
    I would sort the selection array and scan it in reverse order.

    ...
    int[] selection = jList3.getSelectedIndices();
    Arrays.sort(selection);
    for (int i = selection.length; --i >= 0; ){
    ...
    

    UPDATE: the sorting is not required, because the array returned by getSelectedIndices() already is

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

Sidebar

Related Questions

I have this method which does certain checks and applies a color code on
I have this code which will include template.php file from inside each of these
I have this code (which is way simplified from the real code): public interface
I have this code which I have found from google. It's a adapter for
I have this segment of Scala code which defines an ordering and applies it
I have this code which is called at an onChange event of an function
I have this code which gets WP posts, but it gets all the posts
I have this code which compiles and works as expected: class Right {}; class
I have this code which should create a splash image with either no animation
I have this code which is passed to Paypal checkout: <?php foreach ($paypalBasket as

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.