实现ListModel,需要在Change上更新JList

时间:2017-04-05 17:47:49

标签: java jlist listmodel

我有一个实现ListModel的自定义集合。当此集合发生变化时,我需要通知JList。通常您需要fireListDataChanged,但这不是ListModel实施的方法。

我应该如何修改代码以自动更新JList

JList:

this.selectedInterfacesJList.setModel(new TufinInterfaceCollection());

此处收集:

public class TufinInterfaceCollection extends AbstractCollection<TufinInterface> implements ListModel<String> {
    ArrayList<TufinInterface> list=new ArrayList();
    private final ArrayList listeners=new ArrayList();

    @Override
    public boolean remove(Object o) {
        return list.remove(o); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean add(TufinInterface e) {
        return list.add(e); //To change body of generated methods, choose Tools | Templates.
    }

    public TufinInterface get(int i){
        return list.get(i);
    }

    @Override
    public Iterator<TufinInterface> iterator() {
        return list.iterator();
    }

    @Override
    public int size() {
       return list.size();
    }

    @Override
    public int getSize() {
        return size();
    }

    @Override
    public String getElementAt(int i) {
        return get(i).toString();
    }    

    @Override
    public void addListDataListener(ListDataListener ll) {
        this.listeners.add(ll);
    }

    @Override
    public void removeListDataListener(ListDataListener ll) {
        this.listeners.remove(ll);
    }    
}

0 个答案:

没有答案