0

我有一个简单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();
}
}

关于它的奇怪之处在于,它不起作用,只要我在窗口外单击例如,JListgetListModel的正确更新...

我在这里想念什么?

4

2 回答 2

3

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

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

于 2013-04-29T17:30:03.033 回答
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();
    }
}
于 2013-04-30T07:56:30.680 回答