java - Replacing JList through Combobox selection -


i'm creating word search game combobox has different game itemsand jlist shows words found in word list. when user clicks on item in combobox, list should show words associated item. right now, work when select first item, if try change word list selecting second item, adds words without deleting original words. want replace words, not add them on. clear list model in action listener reason isn't working. setanimallist, setspacelist, , setcomplist methods called subclass set 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();         (int = 0; < wordlist2.size(); i++) {             listmodel.addelement(wordlist2.get(i).getword());         }         wordlist2.clear();         }     else if (selected.equals("space")) {         setspacelist(wordlist2);         listmodel.clear();         (int = 0; < wordlist2.size(); i++) {             listmodel.addelement(wordlist2.get(i).getword());         }         wordlist2.clear();     }     else if (selected.equals("computers")) {         setcomplist(wordlist2);         listmodel.clear();         (int = 0; < wordlist2.size(); i++) {             listmodel.addelement(wordlist2.get(i).getword());         }         wordlist2.clear();     } } 

}

absent complete example, might try creating & initializing 3 instances of defaultlistmodel , using wordlist2.setmodel() in combo's listener. related example using comboboxmodel shows idea.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

qt - Errors in generated MOC files for QT5 from cmake -