tags:

views:

297

answers:

2

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(javax.swing.event.ListSelectionEvent evt) {                                                   
    try {
        inputPanel.setEnabled(false);
        inputPanel.setVisible(false);
        inputTextField.setText("");
        inputStatusLabel.setText("");
        // get the questionaireIndex selected
        int questionaireIndex = questionaireNamesList.getSelectedIndex();
        **System.out.println("Questionaire Index: " + questionaireIndex);**
        // check if that questionaire has five questions
        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

+3  A: 

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.

Eddie
If index 4 is unselected then why would getSelectedIndex() return 4 and not -1?
Patrick Kiernan
I edited my answer to explain.
Eddie
You say if index #4 is UNSELECTED it can legally return 2 or -1. Why 2?
Patrick Kiernan
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.
Eddie
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 :)
Patrick Kiernan
+1  A: 

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

TofuBeer