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 data = null;
private final ArrayList indices = new ArrayList();
public FilteredListModel()
{
}
public void setData(List 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?
解决方案
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.