java jlist - AbstractListModel - fireContentsChanged does not work properly

自古美人都是妖i 提交于 2019-12-12 00:49:26

问题


I have a simple ListModel, that is filterable and is used in a JList...

It uses the following code...

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();
}
}

The strange thing about it is, that it is not working, just if I click for example outside the window, the JList with the ListModel get's correctly updated...

What am I missing here?


回答1:


The problem is that the addListDataListener and removeListDataListener methods are empty. This means the JList can no longer attach its listener to the model. The call fireContentsChanged will do nothing, as the super class isn't aware of any listeners.

Either do not override those methods, or make sure you call super.addListDataListener as well.




回答2:


@Robin please 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();
    }
}


来源:https://stackoverflow.com/questions/16280301/java-jlist-abstractlistmodel-firecontentschanged-does-not-work-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!