Vector is synchronized, so I could see an argument for preferring it over unsynchronized ArrayLists. You can modify the Vector that you pass in to the JList constructor, since the constructor just refers to the Vector directly:
public JList(final Vector<? extends E> listData) {
this (
new AbstractListModel<E>() {
public int getSize() { return listData.size(); }
public E getElementAt(int i) { return listData.elementAt(i); }
}
);
}
Since you're using Vector, in theory you can modify the Vector from any Thread without weird stuff happening.
However, from my testing, you need to call JList.updateUI() after you modify it anyway, so I don't see any huge benefits from this approach:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class JListTest {
public JListTest(){
final JFrame frame = new JFrame ("JList Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final Vector<String> v = new Vector<String>();
v.add("one");
v.add("two");
v.add("three");
final JList<String> list = new JList<String>(v);
JButton button = new JButton("Add");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
v.add("four");
list.updateUI();
}
});
frame.add(new JScrollPane(list), BorderLayout.CENTER);
frame.add(button, BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);
}
public static void main(String [] args){
new JListTest();
}
}
So I don't know. This might be a remnant of AWT which is supposed to be thread-safe, but it's often a bit of a waste of time to focus too much on these kinds of questions.
Also, from the DefaultListModel API:
This class loosely implements the java.util.Vector API, in that it implements the 1.1.x version of java.util.Vector, has no collection class support, and notifies the ListDataListeners when changes occur. Presently it delegates to a Vector, in a future release it will be a real Collection implementation.