I want to bind a JList in a JScrollPane to an array , whenever the array c开发者_运维问答hanges the List changes .
The first idea, of course, would by to use one of JList
's constructors and hope that the JList
component is updated synchronously with the array:
public JList(Object[] listData);
public JList(Vector<?> listData);
Obviously, this does not work out. Only, if you use the third non-default constructor
public JList(ListModel model);
and use the default implementation DefaultListModel
and update its elements directly by e.g.
DefaultListModel model = new DefaultListModel();
...
model.setElementAt(value, 25);
you receive a dynamically updated JList
component, by updating the DefaultListModel
.
What Java SE provides is a "fixed-size List backed up the specified array" by the java.util.Arrays.asList(T... a)
method, compare Java SE API.
However, here the support of Java SE breaks. There is no ListModel
implementation that is "backed-up by a List".
I have tried both ways to overcome this:
- implement the
List
interface in a class synchronously updating an underlyingDefaultListModel
- extend
DefaultListModel
, synchronously updating an underlyingList
instance.
Neither way worked.
Therefore, I venture to say that Java SE does not support this feature yet. You have to code your own implementation of JList
synchronized by a List
instance or wait until a new Java distribution comes along, which features JList's
or DefaultListModel's
backed up by List
instances.
精彩评论