I’m having a poblem with my project, basically its a small app that stores synonyms.
I have 2 JPanels populated with two JLists, one for the list of words and one with the correspondent synonyms.
When you click on a word on one JList, the correspondent synonyms appears on the other JList
then there are 2 JTextFields, for adding a word-synonym pair, in one field you insert the word in the other you insert the synonyms, split them creating an array with synonyms.split(” “) and store all in a Map
this is the ListSelectionListener i have added to the word JList
woordenList.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent evt) {
if (evt.getValueIsAdjusting()){
return;
}
getSynoniemen(evt);
}
});
And this is the method that gets the synonyms
private void getSynoniemen(ListSelectionEvent e){
if(e.getValueIsAdjusting()){
return;
}else{
String woord=(String)woordenList.getSelectedValue();
Object[] synArray=(Object[])beheer.getValues(woord);
synoniemenList.setListData(synArray);
}
}
If i add a word and synonyms into the lists when no item is selected it works fine, if i do this operation of adding items while instead, i have a word selected in the word-JList, the method getValues() with a null string is called, throwing an exception. i dont understand why, adding an element on the list also fires a list selection.
Any advice?
Somewhere in your code you must be changing the selected index.
Download and test the ListDemo example for How to Use Lists. When you run the code and “Hire” a person, then the list selection event fires (I added a System.out.println(…) to the listener). Then if you comment out:
the event is not fired. So you have a problem in your code. Compare your code to the working example code to see what if different.
If you need more help then post a SSCCE that demonstrates the problem.