更新时间:2023-10-06 23:24:28
如果您创建自己的 ListModel
,您应该扩展 AbstractListModel
并且在实现您的 addElement
方法时,您需要调用fire 方法(用于通知用户界面进行更新),例如:
If you create your own ListModel
you should extend AbstractListModel
and when implementing your addElement
method, you need to call a fire-method (for notifying the user interface for the update), like:
public void addElement(MyObject obj) {
myArrayList.add(obj);
fireIntervalAdded(this, myArrayList.size()-1, myArrayList.size()-1);
}
您的自定义 ListModel 应如下所示:
You custom ListModel should look something like this:
public class MyListModel extends AbstractListModel {
private final ArrayList<MyObject> myArrayList = new ArrayList<MyObject>();
public void addElement(MyObject obj) {
myArrayList.add(obj);
fireIntervalAdded(this, myArrayList.size()-1, myArrayList.size()-1);
}
@Override
public Object getElementAt(int index) { return myArrayList.get(index); }
@Override
public int getSize() { return myArrayList.size(); }
}