Using the JList.setModel () method with a class as an argument

My ultimate goal is to have a JList that updates its contents at runtime, and I found a solution that works with this post here on SO , however I am curious why my original idea did not.

At the moment, I have something like this setting, and it works:

DefaultListModel default = new DefaultListModel();

for(int i = 0; i < array.size() ; ++i){
   test.addElement(array.get(i));
}
list.setModel(default);

Below was my original plan. I wanted the class that implemented the ListModel to be passed as an argument, hoping it would update the JList.

SomeClass test = new SomeClass(); //Implements ListModel
list.setModel(test);

or

SomeClass test = new SomeClass(); //Implements ListModel
list = new JList(test);

None of these works bother me. Can these last two methods work like this, the code is so much cleaner.

Thank.

+5
source share
3 answers

, ListModel. , , :

fireContentsChanged(...);

AbstractListModel (, , ). JList .

, JList, . . , , JList , JList . , . .

+3

. ?

, JList Gui. , .

+1

Most likely your ListModel implementation is incorrect.

0
source

All Articles