Use the DefaultListModel
. It has the method setElementAt(E element, int index)
and add(int index, E element)
setElementAt(E element, int index)
- Sets the component at the specified index of this list to be the specified element. The previous component at that position is discarded.add(int index, E element)
- Inserts the specified element at the specified position in this list.
You can initialize your JList
with a DefaultListModel
and then use all of the DefaultListModel
s methods
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
Then just use its methods
model.setElementAt("new text", index);
model.add(index, "new text");
See: DefaultListModel javadoc for more methods | How to use Lists tutorial