I'm creating a word search game with a ComboBox that has the different game itemsand a JList that shows the words to be found in the word list. When the user clicks on an item in the combobox, the list should show all the words associated with that item. Right now, it will work when I select the first item, but if I try to change the word list by selecting a second item, it just adds those words without deleting the original words. I want to replace the words, not add them on. I clear the list model in the action listener but for some reason it isn't working. The setAnimalList, setSpaceList, and setCompList methods are called from a subclass to set the values of wordList2.
ArrayList<Word> wordList2 = new ArrayList<Word>(12);
String[] gameStrings = {"Space", "Animals", "Computers"};
JList words;
JComboBox menu;
DefaultListModel listModel;
listModel = new DefaultListModel();
words = new JList(listModel);
menu = new JComboBox(gameStrings);
menu.addActionListener(this);
public void actionPerformed(ActionEvent e) {
String selected = (String)menu.getSelectedItem();
if (selected.equals("Animals")) {
setAnimalList(wordList2);
listModel.clear();
for (int i = 0; i < wordList2.size(); i++) {
listModel.addElement(wordList2.get(i).getWord());
}
wordList2.clear();
}
else if (selected.equals("Space")) {
setSpaceList(wordList2);
listModel.clear();
for (int i = 0; i < wordList2.size(); i++) {
listModel.addElement(wordList2.get(i).getWord());
}
wordList2.clear();
}
else if (selected.equals("Computers")) {
setCompList(wordList2);
listModel.clear();
for (int i = 0; i < wordList2.size(); i++) {
listModel.addElement(wordList2.get(i).getWord());
}
wordList2.clear();
}
}
}