0

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?

Eike Pierstorff
  • 31,996
  • 4
  • 43
  • 62
prom85
  • 16,896
  • 17
  • 122
  • 242
  • 1
    For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Apr 29 '13 at 13:50
  • Actually, I edited my post in the meanwhile and added the whole class... All I do is calling doFilters after updating the data... but that's not working so far... – prom85 Apr 29 '13 at 13:53
  • 1
    *"added the whole class"* An SSCCE might consist of more than one class and your code as is, is ***not an SSCCE.*** Don't get me wrong, it is **short** enough, but look at the rest of the term.. – Andrew Thompson Apr 29 '13 at 13:55

2 Answers2

3

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.

Robin
  • 36,233
  • 5
  • 47
  • 99
  • sorry please can you be little bit concrete, otherwise .......hmmm I'd be use JTable, one column, without JTableHeader rather than bothering with wind in bottle – mKorbel Apr 29 '13 at 19:53
  • @mKorbel What do you mean with more concrete. He should either not override those methods from `AbstractListModel` or at least call the `super` method when overriding those. Now no listeners can be added to the model – Robin Apr 29 '13 at 22:03
  • never ever seen good AbstractListModel, there are a bunch small inconveniences that made things ...., see my deleted post here – mKorbel Apr 30 '13 at 07:58
  • actually, I realised that those methods are causing the problem... and changed my class to extending `DefaultListModel` and everything is working fine... thanks though – prom85 Apr 30 '13 at 09:29
1

@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();
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319