Ho una JList con alcuni elementi. Ho aggiunto un listener per quando un elemento nell'elenco è selezionato. Ecco il codice per ciò che accade quando si seleziona una voce nella lista:Perché le selezioni JList si verificano due volte?
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();
}
}
Come potete sopra ho messo una dichiarazione System.out.print
e ogni volta che clicco su qualcosa nella lista ottengo due delle uscite di quell'elemento , per esempio.
Questionaire Index: 4
Questionaire Index: 4
Questionaire Index: 2
Questionaire Index: 2
Questionaire Index: 0
Questionaire Index: 0
Questionaire Index: 2
Questionaire Index: 2
Qualsiasi idea del perché questo sta accadendo?
Grazie, Patrick
Se l'indice 4 non è selezionato, perché getSelectedIndex() restituisce 4 e non -1? –
Ho modificato la mia risposta per spiegare. – Eddie
Si dice che se l'indice # 4 è NON SELEZIONATO può restituire legalmente 2 o -1. Perché 2? –