Search code examples
javaswinglistenerselectionjlist

Every time I click on a Jlist item, the listener is invoked an increasing number of times even though my code only runs if(!e.getValueIsAdjusting())


I'm using Netbeans to create a jlist using the design tool, so there is auto generated code in there.

I have seen a lot of people getting their listener invoked twice, which was corrected for them by adding the if statement below :

    if(!e.getValueIsAdjusting()){//CODE TO RUN}

However for me, when I click on the items they get fetched more times with each click.

OUTPUT (index in jlist and object at that index in my Arraylist of objects) :

3
com.mycompany.rss.EvenementRssClasse@2c3a9a08
0
com.mycompany.rss.EvenementRssClasse@45fdde8e
0
com.mycompany.rss.EvenementRssClasse@45fdde8e
0
com.mycompany.rss.EvenementRssClasse@45fdde8e
1
com.mycompany.rss.EvenementRssClasse@2cefc4c2
1
com.mycompany.rss.EvenementRssClasse@2cefc4c2
1
com.mycompany.rss.EvenementRssClasse@2cefc4c2
1
com.mycompany.rss.EvenementRssClasse@2cefc4c2
1
com.mycompany.rss.EvenementRssClasse@2cefc4c2

And so on. First click is a single fetch. Second click is 3 fetches, third click is five, etc.

This is the auto-generated declaration of that Jlist in netbeans :

    evenementsJlist = new javax.swing.JList<>();

Auto-generated definition :

    evenementsJlist.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
    evenementsJlist.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
        public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
            evenementsJlistValueChanged(evt);
        }
    });
    evenementsScrollPane.setViewportView(evenementsJlist);

This is my selection handler code :

private void evenementsJlistValueChanged(javax.swing.event.ListSelectionEvent evt) {                                             
    // TODO add your handling code here:
    evenementsJlist.addListSelectionListener(new ListSelectionListener(){
        
        @Override
        public void valueChanged(ListSelectionEvent e){
            if(!e.getValueIsAdjusting()){  
                //This is where I call my function to get the index of the selected event
                selectRssEvnt(evenementsJlist.getSelectedIndex());
            }
        }
    });
}           

My selectRssEvt function. I just want to print out the received index, and then use it to print out the reference to that object in my arrayList of RSS events to validate. :

private void selectRssEvnt(int selIndex){
    System.out.println(selIndex);
    System.out.println(listeDesEvenementsRss.get(selIndex));
}

I have tested selecting my items with the keyboard arrows and they also get printed an increasing number of times.

I'm using a laptop and a touch pad to make my selections, which I assume would be the same as mouseclick DOWN and mouseclick RELEASE.

I have searched quite a lot for this and read all the similar questions, but it remains that apart from ListSelectionMode issues or the popular missing if(!e.getValueIsAdjusting()){} I have not been able to find any solution for this issue of increasing number of calls to the listener with each selection made.


Solution

  • I'm gonna go ahead and answer my own question, using the solution from @madprogrammer, who doesn't seem to be so mad after all.

    Netbeans auto generated the listener, so I didn't need another one.

    I changed my handler code to this in order to solve the problem :

    private void evenementsJlistValueChanged(javax.swing.event.ListSelectionEvent evt) {                                             
        // TODO add your handling code here:
         if(!evt.getValueIsAdjusting()){
            selectRssEvnt(evenementsJlist.getSelectedIndex());
         }
    }