java jlist-AbstractListModel-fireContentsChanged无法正常工作 [英] java jlist - AbstractListModel - fireContentsChanged does not work properly
问题描述
我有一个简单的ListModel
,可以过滤并在JList
...
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();
}
}
奇怪的是,它不起作用,就像我在窗口外单击一样,正确地更新了带有ListModel
get的JList
...
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...
我在这里想念什么?
推荐答案
问题是addListDataListener
和removeListDataListener
方法为空.这意味着JList
不能再将其侦听器附加到模型.调用fireContentsChanged
不会执行任何操作,因为超类不知道任何侦听器.
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.
要么不重写这些方法,要么确保您也调用super.addListDataListener
.
Either do not override those methods, or make sure you call super.addListDataListener
as well.
这篇关于java jlist-AbstractListModel-fireContentsChanged无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!