You cannot directly edit the collection of list values. Instead, you must access the model and then add or remove elements. That, too, is easier said than done. Suppose you want to add more values to a list. You can obtain a reference to the model:
ListModel<String> model = list.getModel();
But that does you no good—as you saw in the preceding section, the
ListModel interface has no methods to insert or remove elements because, after all, the whole point of having a list model is that it does not need to store the elements.
Let’s try it the other way around. One of the constructors of
JList takes a vector of objects:
Vector<String> values = new Vector<String>(); values.addElement("quick"); values.addElement("brown"); ...