How to add element to existing JList

lunar picture lunar · Nov 18, 2011 · Viewed 42.5k times · Source

Part of my code

 ArrayList<Item> i = g.getItems(); 
 Vector itemsVector = new Vector(i); 
 JList items = new JList(iemsVector); 

Later in the code I create new object which I want to add to JList. How can I do that?

Answer

Hovercraft Full Of Eels picture Hovercraft Full Of Eels · Nov 18, 2011

Populate the JList with a DefaultListModel, not a vector, and have the model visible in the class. Then simply call addElement on the list model to add items to it.