Why do JList selections occur twice?
Asked Answered
B

2

7

I have a JList with some items. I've added a listener for when a item in the list is selected. Here is the code for what happens when an item in the list is selected:

private void questionaireNamesListValueChanged(ListSelectionEvent evt) {
    try {
        inputPanel.setEnabled(false);
        inputPanel.setVisible(false);
        inputTextField.setText("");
        inputStatusLabel.setText("");
        int questionaireIndex = questionaireNamesList.getSelectedIndex();

        // Why will this be printed twice?
        System.out.println("Questionaire Index: " + questionaireIndex);

        if (remoteQuestionServer.getQuestionCount(questionaireIndex) == 5) {
            answerQuestionButton.setEnabled(true);
            addQuestionButton.setEnabled(false);
        } else {
            addQuestionButton.setEnabled(true);
            answerQuestionButton.setEnabled(false);
        }
    } catch (RemoteException ex) {
        ex.printStackTrace();
    }
} 

As you can above I put a System.out.print statement in and every time I click on something in the list I get two ouputs for that item, eg.

Questionaire Index: 4
Questionaire Index: 4
Questionaire Index: 2
Questionaire Index: 2
Questionaire Index: 0
Questionaire Index: 0
Questionaire Index: 2
Questionaire Index: 2

Any idea why this is happening?

Thanks, Patrick

Barilla answered 14/3, 2009 at 17:16 Comment(0)
H
13

When you change a selection, one or two events can occur, depending on the implementation. If index #4 is selected and you click on the second item, then the following occurs:

  • First, index #4 is UNSELECTED. Depending on the model, questionaireNamesList.getSelectedIndex() can legally return either 2 or -1.
  • second, index #2 is SELECTED. At this point, questionaireNamesList.getSelectedIndex() will surely return 2.

Thus, there are two events fired. The definition of how these events are generated allows leeway for different JVM implementations do go things slightly differently.

NOTE: You should probably check the value of ListSelectionEvent#getValueIsAdjusting() to see if the event you are processing is one in a series of events. You probably need to ignore all events where this returns true.

Haphtarah answered 14/3, 2009 at 17:24 Comment(7)
If index 4 is unselected then why would getSelectedIndex() return 4 and not -1?Barilla
You say if index #4 is UNSELECTED it can legally return 2 or -1. Why 2?Barilla
In my example, you have just selected #2, so it can legally tell you that nothing is selected, or that the new selection (#2) is the selection.Haphtarah
Ah I see now. So in my case when I have x selected and click y it prints y twice which you have covered here, strange that it works this way though. Anyway thanks for the answer :)Barilla
Correct answer, but I don't believe it is a matter of the implementation of the JVM. It is more a matter of the implementation of the ListSelectionModel, actually it is documented in setValueIsAdjustingAngkor
@CarlosHeuberger: When I said "different JVM implementations" I really meant "JVM" including the full set of libraries as part of the implementation. I didn't mean to imply that the implementation details of the virtual machine itself would affect this.Haphtarah
Sorry, I'm not that good at English, I understand "JVM implementation" as (only) the Java Virtual Machine implementation... The libraries are part of the JRE in my opinion.Angkor
S
2

Further to the answer by Eddie look at the getValueIsAdjusting method on the event.

Survance answered 14/3, 2009 at 17:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.