Reputation: 109
So I have a custom list class that extends AbstractList. Inside my CustomList class, there are two ArrayLists and various methods including add/remove objects methods and methods to return the two lists individually.
Now I am showing my list by the following:
testJList = new JList(CustomList.returnList1().toArray());
My returnList1() methods returns ArrayList.
But when I add an object using my CustomList method of addObject(Object o), the list does not update.
Is there a way to update the list and use my CustomList class?
I know the object is successfully added to the list in CustomList since I made sure to print a message for success and check for errors.
Thank you so much
Upvotes: 1
Views: 553
Reputation: 1112
There is a specific interface for this purpose that implements the publish subscribe pattern called ListModel (as mentioned in the comments), providing a mechanism for the graphics components to get redrawn when needed. List interface isn't designed to provide such a mechanism. DefaultListModel is a built-in implementation of the ListModel.
JList<String> myList = new JList<>();
DefaultListModel<String> myModel = new DefaultListModel<>();
myModel.addElement("My string");
myList.setModel(myModel);
myModel.addElement("Another string");
Upvotes: 1