java jlist - AbstractListModel - fireContentsChanged无法正常工作

时间:2013-04-29 13:46:57

标签: java swing jlist updating

我有一个简单的ListModel,可以过滤并在JList中使用...

它使用以下代码......

public class FilteredListModel extends AbstractListModel
{
private List<LineData> data = null;
private final ArrayList<Integer> indices = new ArrayList<Integer>();

public FilteredListModel()
{
}

public void setData(List<LineData> data)
{
    this.data = data;
    doFilter();
}

public void doFilter()
{
    int oldSize = indices.size();
    indices.clear();

    if (data != null)
    {
        int count = data.size();
        for (int i = 0; i < count; i++)
        {
            IFiltererListObject element = (IFiltererListObject) data.get(i);
            if (element.isVisible())
                indices.add(i);
        }
    }
    fireContentsChanged(this, 0, getSize() - 1);
    if (oldSize > getSize())
        fireIntervalRemoved(this, getSize(), oldSize - 1);
}

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

@Override
public Object getElementAt(int index)
{
    return data.get(indices.get(index));
}

@Override
public void addListDataListener(ListDataListener l)
{
    // TODO Auto-generated method stub
    //doFilter();
}

@Override
public void removeListDataListener(ListDataListener l)
{
    // TODO Auto-generated method stub
    //doFilter();
}
}

关于它的奇怪之处在于,它不起作用,只要我在窗口外点击,JList ListModel得到正确更新...

我在这里缺少什么?

2 个答案:

答案 0 :(得分:3)

问题是addListDataListenerremoveListDataListener方法是空的。这意味着JList无法再将其侦听器附加到模型。调用fireContentsChanged将不执行任何操作,因为超类不知道任何侦听器。

要么不要覆盖这些方法,要么确保也调用super.addListDataListener

答案 1 :(得分:1)

@Robin请DYM ???

import java.util.ArrayList;
import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;

//usage == new JComboBox(new SectionComboBoxModel(new ArrayList());
public class SectionComboBoxModel extends AbstractListModel implements MutableComboBoxModel {

    private static final long serialVersionUID = 1L;
    private Object selectedItem;
    private ArrayList<Object> sections;

    public SectionComboBoxModel(ArrayList<Object> arrayList) {
        sections = arrayList;
    }

    @Override
    public Object getSelectedItem() {
        return selectedItem;
    }

    @Override
    public void setSelectedItem(Object newValue) {
        selectedItem = newValue;
    }

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

    @Override
    public Object getElementAt(int i) {
        return sections.get(i);
    }

    public void setElementAt(Object newValue, int i) {
        this.fireContentsChanged(newValue, i, i);
        this.sections.set(i, newValue);
    }

    @Override
    public void addElement(Object obj) {
        sections.add(obj);
        this.fireIntervalAdded(obj, this.getSize() - 1, this.getSize() - 1);
    }

    @Override
    public void removeElement(Object obj) {
        this.fireIntervalRemoved(obj, sections.indexOf(obj), sections.indexOf(obj));
        sections.remove(obj);
    }

    @Override
    public void insertElementAt(Object obj, int index) {
        sections.add(index, obj);
        this.fireIntervalAdded(obj, index, index);
    }

    @Override
    public void removeElementAt(int index) {
        this.fireIntervalRemoved(sections.get(index), index, index);
        sections.remove(index);

    }

    public void print() {
        System.out.println("\nPrinting List");
        for (int i = 0; i < this.sections.size(); i++) {
            System.out.println(this.sections.get(i));
        }
    }

    public boolean contains(Object o) {
        return sections.contains(o);
    }

    public Object[] toArray() {
        return this.sections.toArray();
    }
}